257

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\*.*
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
faressoft
  • 19,053
  • 44
  • 104
  • 146

11 Answers11

335

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.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • 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! – Arise May 30 '13 at 06:36
  • @sakra 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"" – Furkan Gözükara Jul 11 '14 at 02:13
  • 4
    @MonsterMMORPG prefix the line with `cmd /C` – sakra Jul 11 '14 at 11:41
  • @sakra 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"` – VBAbyMBA Aug 29 '17 at 06:40
  • 1
    worked for me but without the outside quotes: "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\" – user1620090 Jun 19 '19 at 09:54
114

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"
ghbarratt
  • 11,496
  • 4
  • 41
  • 41
Mithil D
  • 1,149
  • 1
  • 7
  • 2
  • 1
    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? – hafizan Jul 07 '20 at 09:04
  • @hafizan 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"' ) ` – henrry Feb 09 '22 at 12:03
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 
jtth
  • 876
  • 1
  • 12
  • 40
rob2universe
  • 7,059
  • 39
  • 54
15

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\*.*")
Dan
  • 273
  • 3
  • 6
14

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).

  • Do underscores in file names count? Like 'file_name' vs 'file name' – SqueakyBeak Jan 08 '20 at 16:26
  • 1
    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. –  Jun 13 '21 at 14:09
13

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\*.*"
JAB
  • 20,783
  • 6
  • 71
  • 80
  • 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\*.*"`? – JAB Jun 16 '11 at 18:09
  • 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 ? – faressoft Jun 16 '11 at 18:29
  • @JAB 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"` – VBAbyMBA Aug 29 '17 at 07:30
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

James Brown
  • 51
  • 1
  • 1
3

Just add Quotation Mark

Example:"C:\Users\User Name"

Hope it got Solved!

Patrick Prakash
  • 500
  • 1
  • 7
  • 17
  • 7
    The same thing has been already said half a dozen times. What extra value does this add? – JJJ May 25 '19 at 06:02
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""
Alan Elston
  • 89
  • 1
  • 4
  • 11
0

You should try using quotes.

cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*"
Jaanus
  • 16,161
  • 49
  • 147
  • 202
  • 2
    This is not reliable. Under certain cases (see http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true - Processing quotation marks) a file "C:\Program.bat" is executed instead. – Micha Wiedenmann Sep 08 '14 at 06:51
-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

Karen
  • 1