60

I want to call

cmd /c "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com" mysolution.sln /build "release|win32"

Unfortunately this does not work, because I get the error:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

As I understand, I need quoting for the spaces and quotes for the |, but I am only allowed to use the quotes once.

Any ideas how to quote this command line call correctly?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
pyfex
  • 1,195
  • 1
  • 8
  • 13

3 Answers3

127

Note the "" at the beginning and at the end!

Run a program and pass a Long Filename

cmd /c write.exe "c:\sample documents\sample.txt"

Spaces in Program Path

cmd /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""

Spaces in Program Path + parameters

cmd /c ""c:\Program Files\demo.cmd"" Parameter1 Param2

Spaces in Program Path + parameters with spaces

cmd /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""

Launch Demo1 and then Launch Demo2

cmd /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""

CMD.exe (Command Shell)

Zombo
  • 1
  • 62
  • 391
  • 407
moskito-x
  • 11,832
  • 5
  • 47
  • 60
  • I can't seem to get this to work with RunAs: `RUNAS /savecred /user:DOMAIN\USER "CMD /K "CD C:\Temp""` ... can you help? – PeterX Apr 24 '13 at 06:35
  • >RUNAS /savecred /user:computername\Administrator "CMD /K CD C:\Temp" – moskito-x Apr 24 '13 at 17:08
  • Docs say to use &&, not & to join commands. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true – nmz787 Jul 14 '14 at 18:26
  • 1
    @nmz787 : that is only `&&` if you have all commands enclosed in quotation marks (for example, `"command&&command&&command"` ). – moskito-x Jul 14 '14 at 19:06
  • 2
    @nmz787, moskito-x, a & b & c always executes a, b and c, but a && b && c makes it conditional. The command following && is only executed if the preceding one succeeded. – Reg Edit Aug 10 '14 at 10:47
  • In case anyone else gets confused like me, this is different from how a .net console app reads the args param. – majjam Jul 21 '16 at 10:11
  • Note that this behaviour is special to *cmd.exe*. If the command starts and ends with quotes, everything inside will be preserved no matter the whitespace or other quotes. One of the "advantages" of the Windows command-line parsing system. – IS4 Dec 18 '17 at 01:29
  • What happens when you have spaces in Program Path and you have parameters with spaces AND a double quote character? It is a password parameter. – Jason L Wharton Dec 12 '18 at 17:12
  • @JasonLWharton : `"...s AND a double quote character?"` an example would be nice. – moskito-x Dec 12 '18 at 20:29
  • Doesn't work for me: `C:\Windows\System32\cmd.exe /k C:\ProgFile\php\php.exe -f C:\ProgFile\Passwords.php | ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" --new-tab %s' – David Spector Jul 08 '23 at 13:11
2

Spaces are used for separating Arguments. In your case C:\Program becomes argument. If your file path contains spaces then add Double quotation marks. Then cmd will recognize it as single argument.

DigviJay Patil
  • 986
  • 14
  • 31
-5

Spaces are horrible in filenames or directory names.

The correct syntax for this is to include every directory name that includes spaces, in double quotes

cmd /c C:\"Program Files"\"Microsoft Visual Studio 9.0"\Common7\IDE\devenv.com mysolution.sln /build "release|win32"

AK65
  • 13