windows 如何使用批处理将工作目录转换为 8.3 短文件名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1333589/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:02:20  来源:igfitidea点击:

How do I transform the working directory into a 8.3 short file name using batch?

windowsbatch-file

提问by Geo

I'm writing a build script, and if the directory the user's building the script contains spaces, everything falls apart. To go around that, I thought of using 8.3 filenames so that drive:\Documents and setttings\whateverbecomes drive:\Docume~1\whatever. The current directory can be found by querying the environment variable %CD%.

我正在编写一个构建脚本,如果用户构建脚本的目录包含空格,那么一切都会分崩离析。为了解决这个问题,我想到使用 8.3 文件名,这样drive:\Documents and setttings\whatever就变成了drive:\Docume~1\whatever. 通过查询环境变量%CD%可以找到当前目录。

How do I transform %CD% into a short file path?

如何将 %CD% 转换为短文件路径?

回答by Preet Sangha

for %f in ("%cd%") do @echo %~sf

Edit: don't forget to use %% if you are using it in batch file.like this

编辑:如果您在批处理文件中使用它,请不要忘记使用 %%。像这样

for %%f in ("%cd%") do @echo %%~sf

On my machine:

在我的机器上:

C:\Users\preet>cd "\Program Files"
C:\Program Files>for %f in ("%cd%") do @echo %~sf
C:\PROGRA~1

Other options:

其他选项:

In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

此外,还增强了 FOR 变量引用的替换。您现在可以使用以下可选语法:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

回答by Bombe

Better: use quote characters (") around all your paths.

更好:"在所有路径周围使用引号字符 ( )。