windows 如何在命令提示符中使用空格?

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

How do I use spaces in the Command Prompt?

windowscmdescaping

提问by faressoft

How can I use spaces in the Windows Command Line?

如何在 Windows 命令行中使用空格?

cmd /C C:\Program Files (x86)\WinRar\Rar.exe a D:\Hello 2\File.rar D:\Hello 2\*.*

回答by sakra

Single quotation marks won't do in that case. You have to add quotation marks around each path and also enclose the whole command in quotation marks:

在这种情况下,单引号不起作用。您必须在每个路径周围添加引号并将整个命令括在引号中:

cmd /C ""C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*""

回答by Mithil D

I just figured out that for a case where the path involves the use of white space characters, for example, when I need to access the app xyzwhich location is :

我刚刚发现,对于路径涉及使用空格字符的情况,例如,当我需要访问xyz位置为的应用程序时:

C:\Program Files\ab cd\xyz.exe

To run this from windows cmd prompt, you need to use

要从 windows cmd 提示符运行它,您需要使用

C:\"Program Files"\"ab cd"\xyz.exe

or

或者

"C:\Program Files\ab cd\xyz.exe"

回答by rob2universe

If double quotes do not solve the issue then try e.g.

如果双引号不能解决问题,请尝试例如

dir /X ~1 c:\

to get a list of alternative file or directory names. Example output:

获取替代文件或目录名称的列表。示例输出:

11/09/2014 12:54 AM             8,065  DEFAUL~1.XML Default Desktop Policy.xml
06/12/2014  03:49 PM    <DIR>          PROGRA~1     Program Files 
10/12/2014  12:46 AM    <DIR>          PROGRA~2     Program Files (x86)

Now use the short 8 character file or folder name in the 5th column, e.g. PROGRA~1 or DEFAUL~1.XML, in your commands. For instance:

现在在您的命令中使用第 5 列中的 8 个字符的短文件或文件夹名称,例如 PROGRA~1 或 DEFAUL~1.XML。例如:

set JAVA_HOME=c:\PROGRA~1\Java\jdk1.6.0_45 

回答by JAB

Enclose the paths containing spaces with double quotes.

用双引号将包含空格的路径括起来。

cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*"

回答by Vaisak Mohan

Try to provide complex pathnames in double-quotes (and include file extensions at the end for files.)

尝试用双引号提供复杂的路径名(并在文件末尾包含文件扩展名。)

For files:

对于文件:

call "C:\example file.exe"

For Directory:

对于目录:

cd "C:\Users\User Name\New Folder"

CMD interprets text with double quotes ("xyz") as one string and text within single quotes ('xyz') as a command. For example:

CMD 将带双引号 ("xyz") 的文本解释为一个字符串,将单引号 ('xyz') 中的文本解释为命令。例如:

FOR %%A in ('dir /b /s *.txt') do ('command')

FOR %%A in ('dir /b /s *.txt') do (echo "%%A")

FOR %%A in ('dir /b /s *.txt') do ('command')

FOR %%A in ('dir /b /s *.txt') do (echo "%%A")

And one good thing, cmd is not* case sensitive like bash. So "New fiLE.txt" and "new file.TXT" is alike to it.

一件好事,cmd 不像 bash 那样区分大小写。所以“New file.txt”和“new file.TXT”是相似的。

*Note: The %%A variables in above case is case-sensitive (%%A not equal to %%a).

*注意:上述情况下的 %%A 变量区分大小写(%%A 不等于 %%a)。

回答by Dan

I prefer to enclose the command in ()which is valid batchwhich makes it a bit easier to read:

我更喜欢将()有效的命令括起来,这样batch更容易阅读:

cmd /C ("C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*")

回答by James Brown

set "CMD=C:\Program Files (x86)\PDFtk\bin\pdftk"
echo cmd /K ""%CMD%" %D% output trimmed.pdf"
start cmd /K ""%CMD%" %D% output trimmed.pdf"

this worked for me in a batch file

这在批处理文件中对我有用

回答by Patrick Prakash

Just add Quotation Mark

只需添加引号

Example:"C:\Users\User Name"

示例:“C:\用户\用户名”

Hope it got Solved!

希望得到解决!

回答by Jaanus

You should try using quotes.

您应该尝试使用引号。

cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*"

回答by Alan Elston

Spaces in the Commend Prompt (in a VBA Shell command code line)

Commend Prompt 中的空格(在 VBA Shell 命令行中)

I had a very similar problem which ended up being a space in the command prompt when automating via VBA to get the contents from the command window into a text file. This Thread was one of many I caught along the way that didn't quite get me the solution.

我有一个非常相似的问题,当通过 VBA 自动化将命令窗口中的内容获取到文本文件时,它最终成为命令提示符中的一个空格。这个线程是我一路上遇到的许多问题之一,但并没有完全让我找到解决方案。

So this may help others with a similar problem: Since the syntax with quotes is always difficult to get right , I think showing some specific examples is always useful. The additional problem you get using the command prompt in VBA via the Shell thing, is that the code line often won't error when something goes wrong: in fact a blink of the black commend window misleads into thinking something was done.

所以这可能会帮助其他有类似问题的人:由于带引号的语法总是很难正确,我认为展示一些具体的例子总是有用的。您通过 Shell 在 VBA 中使用命令提示符遇到的另一个问题是,当出现问题时,代码行通常不会出错:实际上,黑色推荐窗口的闪烁会误导人们认为已经完成了某些事情。

As example… say I have a Folder, with a text file in it like at

例如……说我有一个文件夹,里面有一个文本文件,比如

C:\Alans Folder\test1.txt ( https://imgur.com/FELSdB6)

C:\艾伦文件夹\test1.txt ( https://imgur.com/FELSdB6)

The space there in the folder name gives the problem.

文件夹名称中的空格给出了问题。

Something like this would work, assuming the Folder, AlansFolder, exists

假设文件夹 AlansFolder 存在,这样的事情会起作用

Sub ShellBlackCommandPromptWindowAutomatingCopyingWindowContent()
 Shell "cmd.exe /c ""ipconfig /all > C:\AlansFolder\test1.txt"""
End Sub

This won't work. (It won't error).

这行不通。(它不会出错)。

Sub ShellBlackCommandPromptWindowAutomatingCopyingWindowContent()
 Shell "cmd.exe /c ""ipconfig /all > C:\Alans Folder\test1.txt"""
End Sub

Including quote pairs around the path will make it work

在路径周围包含引号对将使其工作

Sub ShellBlackCommandPromptWindowAutomatingCopyingWindowContent()
 Shell "cmd.exe /c ""ipconfig /all > ""C:\Alans Folder\test1.txt"""""
End Sub

( By the way, if the text file does not exist, then it will be made).

(顺便说一句,如果文本文件不存在,那么它会被制作)。

With the benefit of hindsight, we can see that my solution does tie up approximately with some already given..

事后看来,我们可以看到我的解决方案确实与一些已经给出的解决方案大致相关。

Converting that code line to a manual given command we would have

将该代码行转换为我们将拥有的手动给定命令

ipconfig /all > "C:\Alans Folder\test1.txt"

That seems to work

这似乎有效

This works also

这也有效

ipconfig /all > C:\AlansFolder\test1.txt

This doesn't

这不

ipconfig /all > C:\Alans Folder\test1.txt

This final form also works and ties up with the solution from sacra ….” You have to add quotation marks around each path and also enclose the whole command in quotation marks“ …..

这种最终形式也有效并与来自 sacra 的解决方案联系起来......” 您必须在每个路径周围添加引号并将整个命令括在引号中“.....

cmd.exe /c "ipconfig /all > "C:\Alans Folder\test1.txt""