You are not logged in.
Hi, try this batch file:
@echo %cmdcmdline%
@echo %cmdcmdline:/=%
@echo %cmdcmdline%
This is meant to print the command line that started the current instance of cmd.exe.
Type CMD /K and then run the batch file again. The output is:
CMD /K
CMD K
CMD K
The second line is meant to print the command line with any slashes removed. However, it actually alters the variable! It only seems to do this with the CMDCMDLINE variable. It doesn't do it with any other. Isn't this weird?
Offline
Thanks for sharing my "grief". What I was trying to do was to have a batch file pause, just before exiting, to allow the reader to view the output, but this is only necessary if the batch file was run by double-clicking in Windows Explorer. If the batch file was run from the command line, it would be pointless to have it pause before exiting.
So I needed a way to distinguish how the batch file was run, and noticed that when double-clicked from Explorer the command line contained cmd.exe /c whereas when run from the command prompt it didn't. So I tried this as the last line of the batch file:
if /i not "%CMDCMDLINE:/c=%" == "%CMDCMDLINE%" pause
But to my surprise it didn't work (because of the 'undocumented feature.') So in the end I used the following instead:
setlocal
set C=%CMDCMDLINE%
if /i not "%C:/c=%" == "%C%" pause
I hope this is useful in case anyone else around here wants to use it.
Last edited by klint (23 May 2008 18:16)
Offline
It's never a good idea to modify any of the 'built-in' variables, try changing %DATE% or %TIME% and you will get some really confusing results.
Using SETLOCAL and your own variable, as you have above, is the right way.
Offline
It's never a good idea to modify any of the 'built-in' variables, try changing %DATE% or %TIME% and you will get some really confusing results.
Using SETLOCAL and your own variable, as you have above, is the right way.
Agreed, but the whole point of this thread is that I was not changing its value!
When you do %VARIABLE:substring=replacement% you are not modifying the value of VARIABLE. It is only an expression that returns the value of the variable with the specified substitution. The VARIABLE itself remains unchanged.
Unless that variable happens to be CMDCMDLINE. That's why it's a bug.
Offline
Ah yes I see what you mean, interestingly the bug still crops up even if you specify SETLOCAL
Offline