258

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

11 Answers 11

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.

5
  • 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, 2013 at 6: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"" Jul 11, 2014 at 2:13
  • 4
    @MonsterMMORPG prefix the line with cmd /C
    – sakra
    Jul 11, 2014 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, 2017 at 6: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\" Jun 19, 2019 at 9: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"
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 7, 2020 at 9: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 9, 2022 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 
2
  • This is the easy way. No double-double quote mucky-muck involved. Feb 25, 2015 at 20:34
  • 1
    Are you aware that short 8.3 is an NTFS option and can be disabled? Don't rely on it.
    – Zan Lynx
    May 28, 2019 at 2:26
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\*.*")
15

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

2
  • Do underscores in file names count? Like 'file_name' vs 'file name' Jan 8, 2020 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.
    – user5927645
    Jun 13, 2021 at 14:09
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\*.*"
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\*.*"?
    – JAB
    Jun 16, 2011 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, 2011 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, 2017 at 7: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

3

Just add Quotation Mark

Example:"C:\Users\User Name"

Hope it got Solved!

1
  • 7
    The same thing has been already said half a dozen times. What extra value does this add?
    – JJJ
    May 25, 2019 at 6: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""
0

You should try using quotes.

cmd /C "C:\Program Files (x86)\WinRar\Rar.exe" a "D:\Hello 2\File.rar" "D:\Hello 2\*.*"
1
-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

Not the answer you're looking for? Browse other questions tagged or ask your own question.