windows 使用 cmd.exe 将长文件名转换为短文件名 (8.3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10227144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert long filename to short filename (8.3) using cmd.exe
提问by user1251007
I am trying to convert a long filename to a short filename (8.3) on Windows.
我试图在 Windows 上将长文件名转换为短文件名 (8.3)。
A batch-file with a command line argument works as intended:
带有命令行参数的批处理文件按预期工作:
short.bat:
短.bat:
@echo OFF
echo %~s1
calling short.bat C:\Documents and Settings\User\NTUSER.DAT
returns C:\DOCUM~1\USER\NTUSER.DAT
调用short.bat C:\Documents and Settings\User\NTUSER.DAT
返回C:\DOCUM~1\USER\NTUSER.DAT
However, I don't like having an extra .bat-file for this. I would rather call cmd.exe
with the whole command from a ruby script.
How can I do this?
但是,我不喜欢为此使用额外的 .bat 文件。我宁愿cmd.exe
从 ruby 脚本中调用整个命令。我怎样才能做到这一点?
As an intermediate step I tried to hardcode the path in the batch-file, but that does not work:
作为中间步骤,我尝试对批处理文件中的路径进行硬编码,但这不起作用:
short1.bat:
short1.bat:
@echo OFF
SET filename="C:\Documents and Settings\User\NTUSER.DAT"
echo %filename%
echo %~sfilename%
echo %filename%
works, but echo %~sfilename%
gives the following error:
echo %filename%
有效,但echo %~sfilename%
出现以下错误:
The following usage of the path operator in batch-parameter
substitution is invalid: %~sfilename%
For valid formats type CALL /? or FOR /?
If short1.batworks, how can I convert this into a one-liner that can be called with cmd.exe \c ...
?
如果short1.bat有效,我如何将其转换为可以用 调用的单行cmd.exe \c ...
?
There is another question (how to get DOS path instead of Windows path), however that one is specifically asking for the path of the current directory.
还有另一个问题(如何获取 DOS 路径而不是 Windows 路径),但是该问题专门询问当前目录的路径。
回答by dbenham
cmd /c for %A in ("C:\Documents and Settings\User\NTUSER.DAT") do @echo %~sA
回答by Robert Louis Murphy
Replace the filename.txt to the filename you want to convert to 8.3
将 filename.txt 替换为要转换为 8.3 的文件名
dir /x filename.txt
You will then have to split the result with whitespace as your delimiter (\s in regex). Then the value with the ~ is your short filename. If your filename is short to begin with, then you won't find a string containing a ~.
然后,您必须使用空格作为分隔符(正则表达式中的 \s)分割结果。然后带有 ~ 的值是您的短文件名。如果您的文件名开头很短,那么您将找不到包含 ~ 的字符串。
回答by Celes
Here is an example that read in the registry the location of your "appdata\local" folder and convert it to short path:
这是一个示例,它在注册表中读取“appdata\local”文件夹的位置并将其转换为短路径:
cls
@echo off
cd /d "%~dp0"
chcp 65001 >nul
for /f "skip=1" %%a in ('"wmic useraccount where name='%USERNAME%' get sid"') do (
for %%b in (%%a) do set current_SID=%%b
)
set current_username=%USERNAME%
set current_userprofile=%USERPROFILE%
set key_to_read=HKEY_USERS\%current_SID%\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
set value_to_read=Local AppData
rem If value_to_read contains ? space(s) set tokens to 2+?
for /f "usebackq eol= tokens=3,* delims= " %%a in (`reg query "%key_to_read%" /v "%value_to_read%" 2^>nul ^| find "%value_to_read%"`) do (
set value_type=%%a
set data_read=%%b
)
set data_read=%data_read:USERPROFILE=current_userprofile%
call set "data_read=%data_read%"
set current_local_appdata=%data_read%
set current_local_appdata_temp=%current_local_appdata%\Temp
echo %current_local_appdata_temp%
for %%a in ("%current_local_appdata_temp%") do set "current_local_appdata_temp_short=%%~sa"
echo %current_local_appdata_temp_short%
pause
exit