cmd /c and the & symbol in file path - windows

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""

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"

quotes inside quotes with windows' cmd

I have the following command set as a custom URL protocol in windows registry:
cmd /V:ON /C "SET r=%1 & SET s=!r:jhvnc:=! & start C:\Program Files\uvnc bvba\UltraVNC\vncviewer.exe !s:_= !"
This is not working because the path to the exe file has a space.
Normally, I'd use quotes:
cmd /V:ON /C "SET r=%1 & SET s=!r:jhvnc:=! & start "C:\Program Files\uvnc bvba\UltraVNC\vncviewer.exe" !s:_= !"
but the command is already in quotes. I tried escaping the quotes with double quotes, but that did not work.
How can I achieve this?
Simplest work around is use the historic short folder name
That should be C:\PROGRA~1\UVNCBV~1\
check using dir /x /a:d C:\PROGRA~1\U*
cmd /V:ON /C "SET r=%1 & SET s=!r:jhvnc:=! & start C:\PROGRA~1\UVNCBV~1\UltraVNC\vncviewer.exe !s:_= !"

cmd set env and run script

I want to run from powershell a batch script that needs a environment variable before run.
This is what I tried.
cmd.exe /c SET ENV_BASE_DIR='C:\Program Files\XY\Z 12.3' "&" "C:\Users\Admin\Desktop\file.bat /d /p"
To avoid quoting headaches, consider (temporarily) setting the environment variable from PowerShell and then invoking the batch file directly:
$env:ENV_BASE_DIR = 'C:\Program Files\XY\Z 12.3'
C:\Users\Admin\Desktop\file.bat /d /p
$env:ENV_BASE_DIR = $null # remove the env. var again.
If you do want to solve this with a cmd.exe one-liner:
cmd.exe /c set ENV_BASE_DIR='C:\Program Files\XY\Z 12.3' `& C:\Users\Admin\Desktop\file.bat /d /p
The & metacharacter is more simply escaped with ` (backtick), PowerShell's escape character.
The batch file path and its arguments are passed as individual arguments.
As for what you tried:
Ultimately, the only problem with your command line was that you put double quotes around C:\Users\Admin\Desktop\file.bat /d /p as a whole, which caused cmd.exe to consider the entire string the executable path.

vbscript error on run command with long argument

I need execute a script on vbs
I have no problem run the program.exe with this script
objShell.Run("%SystemDrive%\temp\program.exe")
However the program can support the arguement for silent installation
I run the following command manually on cmd is
%SystemDrive%\temp\program.exe /s /v"MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG="CFG_GRP" ICG="ICG_GRP" REBOOT=Force /qn""
so I put this command on the script but get exception error ')'
objShell.Run("%SystemDrive%\temp\program.exe /s /v"MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG="CFG_GRP" ICG="ICG_GRP" REBOOT=Force /qn""")
any idea?
Try this
Command = "cmd /c %SystemDrive%\temp\program.exe /s /v""MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG=""CFG_GRP"" ICG=""ICG_GRP"" REBOOT=Force /qn"""""
objShell.Run Command
or
objShell.Run "cmd /c %SystemDrive%\temp\program.exe /s /v""MS=1.1.1.1 SF= %SystemDrive%\temp\cert.ssl -l*v+! %temp%\install.log IP=False CFG=""CFG_GRP"" ICG=""ICG_GRP"" REBOOT=Force /qn"""""
Either way will work the issue is the incorrectly formed string.
Because quotes " denote the start and end of a string when used inside a string they have to be escaped by doubling them "", otherwise VBScript will think it's the end of the string and throw a syntax error.

cmd.exe /k switch

I am trying to switch to a directory using cmd and then execute a batch file
e.g.
cmd /k cd "C:\myfolder"
startbatch.bat
I have also tried (without success)
cmd cd /k cd "C:\myfolder" | startbatch.bat
Although the first line (cmd /k) seems to run ok, but the second command is never run. I am using Vista as the OS
Correct syntax is:
cmd /k "cd /d c:\myfolder && startbatch.bat"
ssg already posted correct answer. I would only add /d switch to cd command (eg. cd /d drive:\directory). This ensures the command works in case current directory is on different drive than the directory you want to cd to.
cmd cd /k "cd C:\myfolder; startbatch.bat"
or, why don't you run cmd /k c:\myfolder\startbatch.bat, and do cd c:\myfolder in the .bat file?
I can't see an answer addressing this, so if anyone needs to access a directory that has space in its name, you can add additional quotes, for example
cmd.exe /K """C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"" & powershell.exe"
From PowerShell you need to escape the quotes using the backquote `
cmd.exe /K "`"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat`" & powershell.exe"
Notice the escaped quotes
`"
inside the path string:
"`"C:\my path\`""
This will execute the proper command in cmd, i.e. the path surrounded with quotes which should work.
The example command above will initialise the MSVC developer command prompt and go back to PowerShell, inheriting the environment and giving access to the MSVC tools.
You can use & or && as commands separator in Windows.
Example:
cmd cd /K "cd C:\myfolder && startbatch.bat"
I give this as an answer because I saw this question in a comment and cannot comment yet.
cmd /k "cd c:\myfolder & startbatch.bat"
works, and if you have spaces:
cmd /k "cd "c:\myfolder" & startbatch.bat"
As I understand it, the command is passed to cmd as "cd "c:\myfolder" & startbatch.bat", which is then broken down into cd "c:\myfolder" & startbatch.bat at which point the remaining " " takes care of the path as string.
You can also use &&, | and || depending on what you want to achieve.

Resources