quotes inside quotes with windows' cmd - windows

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:_= !"

Related

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.

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

Difference in Delayed expansion of ERRORLEVEL on cmd prompt and win32_process

cmd /V:ON /c dir c:\<some non existing directory> & echo %ERRORLEVEL%
Volume in drive C is PC COE Volume Serial Number is 9C37-D0B7
Directory of c:\
File Not Found
0
Lets run the same command using ! to expand the ERRORLEVEL (delayed expansion is enabled)
cmd /V:ON /c dir c:\ERt & echo !ERRORLEVEL!
Volume in drive C is PC COE
Volume Serial Number is 9C37-D0B7
Directory of c:\
File Not Found
!ERRORLEVEL!
It prints !ERRORLEVEL!.
This does work fine when I run the command using a WMI win32_process create command and proper error is returned using the !ERRORLEVEL! variable
What difference it makes when executing in a cmd prompt and executing using WMI win32_process.?
By using
cmd /V:ON /c dir c:\ERt & echo !ERRORLEVEL!
the new command process started with cmd /V:ON executes just the command dir c:\ERt and then closes and the second command echo !ERRORLEVEL! is executed by current command process on which delayed expansion is not enabled.
The command line
cmd /V:ON /c "dir c:\ERt & echo !ERRORLEVEL!"
must be used to run dir c:\ERt AND echo !ERRORLEVEL! in new command process before exiting this command process.
The double quotes around entire command line to execute in new command process makes the difference.
Without the double quotes around command line to execute in new command process the current command process interprets the line with the two commands as when typing
cmd /V:ON /c dir c:\ERt
echo !ERRORLEVEL!
Also possible would be
cmd /V:ON /c dir c:\ERt ^& echo !ERRORLEVEL!
Now & operator is escaped for current command process being interpreted as literal character and therefore the entire line is executed with caret character ^ removed by the new command process.

How change PATH env in cmd.exe /c in Windows

I want run something like this:
cmd /c "set ""Path=V:\;%Path%"" & ECHO. %Path% & PAUSE"
But there is a problem: the command line window appears only for a moment, then disappears. I create a test .bat (like echo.1: %1 and 2,3...) to check out how it behave and...
0: "V:\test.bat"
1: /c
2: "set ""Path=V:\;
3:
(4-9 the same)
When I replace % with for example 5, second argument look like "set ""Path=V:\;5Path5"" & ECHO. 5Path5 & PAUSE", but it is not what I want.
I think, there may be problem with parsing %Path% inside this shell command.
I tried aslo:
cmd /c "set ""Path=V:\;%%Path%%"" & ECHO. %%Path%% & PAUSE"
cmd /c "set ""Path=V:\;%%%Path%%%"" & ECHO. %%%Path%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%Path%%%%"" & ECHO. %%%%Path%%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%%Path%%%%%"" & ECHO. %%Path%%%%% & PAUSE"
cmd /c "set ""Path=V:\;%%%%%%%%Path%%%%%%%%"" & ECHO. %%%%%%%%Path%%%%%%%% & PAUSE"
cmd /c "set ""Path=V:\;^%Path^%"" &ECHO.^%Path^%&PAUSE"
cmd /c "set ""Path=V:\;^%%Path^%%"" &ECHO.^%Path^%&PAUSE"
cmd /c "set ""Path=V:\;\%Path\%"" &ECHO.\%Path\%&PAUSE"
I will editing post during my next tests...
Edit:
I just noticed that I cannot even use simply: cmd /c "echo %PATH% & PAUSE"
I come to the solution. What I needed:
cmd /c "set ""Path=V:\;%Path:~0%"" &ECHO.%Path:~0%&PAUSE"
It's look like Windows replace %PATH% with this env-variable content (more paths by semicolons), even in Run (Win+R), CMD (.exe), ShellExec (wscript/winapi). So it can not be used as part of argument in shortcut (i needed to).
That doesn't seem to work because xx is evaluated before cmd is started
cmd /c "set xx=yyyyyy & echo %xx% & PAUSE"
That proves that it works, the variable is properly set:
cmd /c "set xx=yyyyyy & set xx & PAUSE"
output:
xx=yyyyyy
Other way to escape the %:
cmd /c "set xx=yyyyyy & ECHOxx.bat & PAUSE"
(ECHOxx.bat is a script which does echo %xx%)
output
yyyyyy
For the path, it's slightly complex as you noticed yourself, but it works fine too:
cmd /c "set PATH=%CD%\subdir;"%PATH%" & echoxx.bat"
it works: echoxx.bat had been moved in the subdir directory, and it is found.

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