Correct quoting for cmd.exe for multiple arguments - cmd

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?

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)

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.

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"

Related

How do I format the Target field of a Windows SendTo shortcut to quote the paths I'm passing to it?

OS: Windows 10
I have a Windows shortcut in my SendTo folder with the following Target:
C:\Windows\System32\cmd.exe /c "C:\Program Files\Path\To\Executable.exe"
This works just fine if I pass it a file or files without spaces in their filenames. But if I pass it files with spaces, I get the following error:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
(I changed it to /k to see the error. /c terminates right away, so I couldn't see the error message.)
If I launch it from inside a CMD session, I would do:
C:\Windows\System32\cmd.exe /c ""C:\Program Files\Path\To\Executable.exe" "path to/first" "path to/second""
But I'm not sure how to tell the Target field to do that. I tried:
C:\Windows\System32\cmd.exe /c ""C:\Program Files\Path\To\Executable.exe" "%1" "%2" "%3""
But that passed a literal %1 as the first argument, a literal %2 as the second argument, and then the file that I right-clicked and chose "Send To -> MyShortcut", prepended by a literal "%3 " as the third argument.
I need to be able to pass at most three arguments.
cmd.exe is stupid and will strip quotes if the command starts and ends with quotes.
Change the shortcuts command to C:\Windows\System32\cmd.exe /c if 1==1 "C:\Program Files\Path\To\Executable.exe"

cmd /c and the & symbol in file path

I ran into a problem, where I have a batch file located in a directory that has the and symbol in it.
When I try to run the cmd /c on the batch file the and symbol messes up the cmd program even though the path is double quoted.
C:\>cmd /c "C:\This & That\batch.bat"
'C:\This' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.
I'm not sure what to do. I tried escaping the & , using ^& but that doesn't seem to work as I'm all-ready in double quotes.
If I run the same command line from a directory that does not have an & symbol it works fine.
C:\>cmd /c "C:\This and That\batch.bat"
C:\>echo "Hi There"
"Hi There"
Thanks for any help.
so this would be the best way.
You also need to escape the & with ^ and run double double quotes.
cmd /c ""C:\This ^& That\batch.bat""
or you can set a location variable and use && to run the set and cmd /c after each other.
set "location=C:\This ^& That" && cmd /c ""%location%\batch.bat""

Proper escaping quotes from bash to batch

I'm trying to start from a cygwin-bash context a batch process.
But my quotes are removed or escaped with backslashes.
Sample:
npp="\"$(cygpath -w "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe")\""
echo "$npp"
echo cmd /c "echo start \"\" $npp"
cmd /c "echo start \"\" $npp"
Output:
"C:\Program Files (x86)\Notepad++\notepad++.exe"
cmd /c echo start "" "C:\Program Files (x86)\Notepad++\notepad++.exe"
start \"\" \"C:\Program Files (x86)\Notepad++\notepad++.exe\"
The first and second line is the expected output.
But in the third line there are unwanted backslashes.
I suppose that the bash shell added these slashes always, but later they are removed by the bash shell again, but in the case of a batch-cmd context there is no process to remove the backslashes.
My question is, how to avoid the backslashes or how to remove them in the batch context?
I don't have a bash instance at hand, but tested on busybox (I know, not the same, just a test)
W:/ $ word='"C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"'
W:/ $ eval "cmd /c echo start \"\" $word"
start "" "C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"
W:/ $ word="C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"
W:/ $ eval "cmd /c echo start \"\" \"$word\""
start "" "C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"
To start NPP in notepad mode with foo.txt opened.
Use the cygpath --dos option, and the cygstart --verbose option:
cygstart --verbose --wait $(cygpath --dos "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe") -multiInst -nosession foo.txt
or let cygstart do the cygpath for us:
cygstart --verbose --wait "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -nosession foo.txt
or just use cmd if it is in your path, otherwise:
/cygdrive/c/Windows/system32/cmd.exe /c start '""' /I /WAIT 'c:\Program Files (x86)\Notepad++\notepad++.exe' -multiInst -nosession -noPlugin -notabbar foo.txt
I found one solution, but it seems to be a bit ugly.
npp="$(cygpath -w "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe")"
cmd /c "set q=\"\" & (call set q=%q:~1,1%) & call echo start %q%%q% %q%$npp%q%"
The second line simply creates in the variable q a single quote sign.
First q is set to \"\" and the (call set q=%q:~1,1%) sets q with the second character of the previous content of q.
And the call ...%q% uses these quotes.
But as said before, this seems not to be a desirable solution.

startup batch file hangs up on second command

I created a startup bat file that looks like this
taskkill /im RemoteDesktopManager.exe
C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
"C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
"C:\Program Files\Mozilla Firefox\firefox.exe"
it does the first and second, but won't go any further, they all exist
how can I fix this?
update : I tried suggestion provided by #phd443322 and wrote this:
taskkill /im RemoteDesktopManager.exe
start "" /w C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
start "" /w "C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
start "" /w "C:\Program Files\Mozilla Firefox\firefox.exe"
intrestingly each command still waits for that program to be closed to continue to the next.
so why still not working?
Below there is a working Batch file, as first advised by phd443322:
taskkill /im RemoteDesktopManager.exe
start "" C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
start "" "C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
start "" "C:\Program Files\Mozilla Firefox\firefox.exe"
Batch files wait for programs to exit unlike interactive. These are the rules documented in the Start command.
If Command Extensions are enabled, external command invocation
through the command line or the START command changes as follows:
non-executable files may be invoked through their file association just
by typing the name of the file as a command. (e.g. WORD.DOC would
launch the application associated with the .DOC file extension).
See the ASSOC and FTYPE commands for how to create these
associations from within a command script.
When executing an application that is a 32-bit GUI application, CMD.EXE
does not wait for the application to terminate before returning to
the command prompt. This new behavior does NOT occur if executing
within a command script.
When executing a command line whose first token is the string "CMD "
without an extension or path qualifier, then "CMD" is replaced with
the value of the COMSPEC variable. This prevents picking up CMD.EXE
from the current directory.
When executing a command line whose first token does NOT contain an
extension, then CMD.EXE uses the value of the PATHEXT
environment variable to determine which extensions to look for
and in what order. The default value for the PATHEXT variable
is:
.COM;.EXE;.BAT;.CMD
Notice the syntax is the same as the PATH variable, with
semicolons separating the different elements.
When searching for an executable, if there is no match on any extension,
then looks to see if the name matches a directory name. If it does, the
START command launches the Explorer on that path. If done from the
command line, it is the equivalent to doing a CD /D to that path.

How to use the start command in a batch file?

I have a batch file that starts an app with a lot of command-line parameters:
"C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\WebDev.WebServer40.exe" /port:1672 /path:"C:\Code.Net\My App\Iteration 6\REL_6.8.806_PerfEnhanceV\Fusion\Code\CC.Fusion\CC.Fusion.Services" /vpath:"/FusionServices"
The problem is that when I run the batch file, the DOS window stays up until the command completes and I would like it to go away. So I tried using the start command, but placing it in front, like this:
start "C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\WebDev.WebServer40.exe" /port:1672 /path:"C:\Code.Net\My App\Iteration 6\REL_6.8.806_PerfEnhanceV\Fusion\Code\CC.Fusion\CC.Fusion.Services" /vpath:"/FusionServices"
But I get an error stating that Invalid switch - "/port:1672"
I have also tried escaping the double quotes, but I was not successful.
How do I fix that?
An extra pair of rabbits' ears should do the trick.
start "" "C:\Program...
START regards the first quoted parameter as the window-title, unless it's the only parameter - and any switches up until the executable name are regarded as START switches.
I think this other Stack Overflow answer would solve your problem: How do I run a bat file in the background from another bat file?
Basically, you use the /B and /C options:
START /B CMD /C CALL "foo.bat" [args [...]] >NUL 2>&1

Resources