windows 如何将有空间的目录路径传递给windows shell?

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

How to pass directory path that have space to windows shell?

windowswindows-shell

提问by karikari

I am using IEcapt.exe to capture website snapshot. The problem is, it cannot handle path directory that have space. Like this:

我正在使用 IEcapt.exe 来捕获网站快照。问题是,它无法处理有空间的路径目录。像这样:

c:\program files\

Is there any way how I can pass directory like this to make it work?

有什么办法可以传递这样的目录以使其工作吗?

回答by kojiro

Usually just double-quoting windows paths will work:

通常只需双引号 Windows 路径即可:

IEcapt.exe "C:\Program Files\some path"

If that really doesn't work, you might be able to get away with using the 8.3 filenameor, in the case of program files, the %programfiles%variable.

如果这真的不起作用,您可能可以使用8.3 文件名,或者在程序文件的情况下,使用%programfiles%变量。

回答by 0xC0000022L

See the reply from kojiro. However, here is a trick if you really aren't able to use blank spaces or long file names, because the tools you call do not support that. I've had this case with some build tools from MS.

见小次郎的回复。但是,如果您真的无法使用空格或长文件名,这里有一个技巧,因为您调用的工具不支持。我用 MS 的一些构建工具遇到过这种情况。

Since short filenames are not allowed to contain blank spaces, we can use:

由于不允许短文件名包含空格,我们可以使用:

%~sI

... where Iis the number of the parameter to the current sub (or the script). Since this is a feature that only works on numbered parameters, you may have to use an indirection, such as:

... 其中I是当前子(或脚本)的参数编号。由于这是一项仅适用于编号参数的功能,因此您可能必须使用间接寻址,例如:

call :mysub "%ProgramFiles%"

:mysub
set VARIABLE=%~s0
goto :eof

回答by ZZZ

kojiro's answer works. But in my case I needed to

kojiro 的回答有效。但就我而言,我需要

IEcapt.exe "%programfiles%\some path"

instead of

代替

IEcapt.exe %programfiles%\some path

The second example works with echocommand to print the entire path on screen for example, but I needed the first example to pass it correctly to my script.

例如,第二个示例使用echo命令在屏幕上打印整个路径,但我需要第一个示例将其正确传递给我的脚本。

回答by Thom Ives

I wish that I could have used one of the methods above. However, I could only use the old DOS short names.

我希望我可以使用上述方法之一。但是,我只能使用旧的 DOS 短名称。

DOS file names longer than 8 characters were truncated with ~number for files. For example, C:\Program Files would be c:\progra~1, and C:\Program Files (x86) would be c:\progra~2.

超过 8 个字符的 DOS 文件名被文件的 ~number 截断。例如,C:\Program Files 将是 c:\progra~1,而 C:\Program Files (x86) 将是 c:\progra~2。

You can find the short names for files using DIR /Xcommand.

您可以使用DIR /X命令找到文件的短名称。

I needed this specifically to launch ConEmu from within Atom text editor.

我特别需要它来从 Atom 文本编辑器中启动 ConEmu。

I derived this approach from Sachleen's Answer HERE

我从 Sachleen's Answer HERE 中得出了这种方法