How do I use spaces in the Command Prompt?
Asked Answered
K

11

266

How can I use spaces in the Windows Command Line?

cmd /C C:\Program Files (x86)\WinRar\Rar.exe a D:\Hello 2\File.rar D:\Hello 2\*.*
Kalasky answered 16/6, 2011 at 17:33 Comment(0)
C
346

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\*.*""

See also the cmd.exe remarks section.

Carolecarolee answered 16/6, 2011 at 20:14 Comment(5)
Yes, every path should be enclosed by quotation marks. Also, the whole command had to be enclosed again by another pair of quotation marks. Now it works! Thanks!Osteoid
@Carolecarolee not working for me either . ""C:\Program Files\WinRAR\WinRAR.exe" a "C:\veri tabani yedekler\Dedicated_Pokemon_Pets_DB_Backup_2014_7_10_7_2.rar" -ri1 -mt2 -m5 "C:\veri tabani yedekler\Dedicated_Pokemon_Pets_DB_Backup_2014_7_10_7_2.bak""Stalag
@MonsterMMORPG prefix the line with cmd /CCarolecarolee
@Carolecarolee I want to exclude file having name with spaces CEEMEA & LATAM.doc. What should I do? "C:\Program Files\WinRAR\rar" a -agmmddyy -x*CEEMEA & LATAM.doc ".rar"Intendant
worked for me but without the outside quotes: "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\"Wimer
S
118

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 xyz which location is :

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

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

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

or

"C:\Program Files\ab cd\xyz.exe"
Sielen answered 8/11, 2013 at 13:16 Comment(2)
I tried this but doesn't seem to work in my case: python ""C:\Program Files (x86)\Jenkins\workspace\Map Checker Unit Test\sqs-poller\node_modules\map-checker\python\unit_test.py" -d "C:\Program Files (x86)\Jenkins\workspace\Map Checker Unit Test\MAP_CHECK_TMP\#Test Case" -o "C:\Program Files (x86)\Jenkins\workspace\Map Checker Unit Test\MAP_CHECK_TMP\#Test Case" -o2 "C:\Program Files (x86)\Jenkins\workspace\Map Checker Unit Test\MAP_CHECK_TMP\#Test Case\testcase_results.csv"" -l "C:\Program Files (x86)\Jenkins\workspace\Map Checker Unit Test\MAP_CHECK_TMP" -restore" . can help?Insouciance
@Insouciance You can use os python library and run all scripts in cmd by python one by one. write all path files (must use / as shown as bellow ) in a list and with for run them one by one . Like os.system( 'python "C:/Program Files (x86)/Jenkins/workspace\Map Checker Unit Test/sqs-poller/node_modules/map-checker/python/unit_test.py"' ) Lazarus
U
32

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:

set JAVA_HOME=c:\PROGRA~1\Java\jdk1.6.0_45 
Unstudied answered 6/1, 2015 at 3:36 Comment(2)
This is the easy way. No double-double quote mucky-muck involved.Spiracle
Are you aware that short 8.3 is an NTFS option and can be disabled? Don't rely on it.Animation
P
17

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

cmd /C ("C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*")
Parquetry answered 27/2, 2016 at 1:10 Comment(0)
C
16

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:

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.

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

Cresa answered 17/2, 2016 at 21:1 Comment(2)
Do underscores in file names count? Like 'file_name' vs 'file name'Gayl
underscores do not raise problems, file_name is treated as single word. But if your file's name is file name with a space, you have to use "file name". Otherwise, the interpreter reads the part file only, the space acts as a delimiter, following that it treats the next word as another argument. This leads to error if the second argument was not expected in that form.Cresa
S
12

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\*.*"
Sibel answered 16/6, 2011 at 17:34 Comment(3)
Then the problem lies in what the argument syntax of Rar.exe actually is. You might be using it wrongly. Maybe try "D:\Hello 2\" or "D:\Hello 2" instead of "D:\Hello 2\*.*"?Sibel
cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a D:\Hello\File.rar D:\Hello*.* (It doesn't have spaces and it is work) but how to deal with spaces ?Kalasky
@Sibel if you need to exclude file having name with spaces like CEEMEA & LATAM.doc. What should it be? "C:\Program Files\WinRAR\rar" a -x*CEEMEA & LATAM.doc ".rar"Intendant
I
5
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

Immobilize answered 29/9, 2015 at 17:55 Comment(0)
K
3

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

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.

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.

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

C:\Alans Folder\test1.txt ( https://i.stack.imgur.com/UiCVF.jpg )

The space there in the folder name gives the problem.

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

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 “ …..

cmd.exe /c "ipconfig /all > "C:\Alans Folder\test1.txt""
Kwang answered 15/12, 2019 at 17:42 Comment(0)
A
2

Just add Quotation Mark

Example:"C:\Users\User Name"

Hope it got Solved!

Appanage answered 25/5, 2019 at 5:35 Comment(1)
The same thing has been already said half a dozen times. What extra value does this add?Industrials
V
0

You should try using quotes.

cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*"
Vaccine answered 16/6, 2011 at 17:36 Comment(1)
This is not reliable. Under certain cases (see microsoft.com/resources/documentation/windows/xp/all/proddocs/… - Processing quotation marks) a file "C:\Program.bat" is executed instead.Luigiluigino
B
-4

It can solve this problem by cd command, this command understand spaces without double quotes and you can call any program this way for example:

C:\Windows\system32>cd c:\Program Files\MongoDB\Server\3.2\bin

c:\Program Files\MongoDB\Server\3.2\bin>mongo now command prompt call mongo.exe

Benedicite answered 17/3, 2016 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.