#1 23 May 2008 17:11

klint
Member
Registered: 19 Mar 2008
Posts: 11

Has cmd.exe gone completely mad or what?

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

#2 23 May 2008 17:17

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,093

Re: Has cmd.exe gone completely mad or what?

I see what you mean.  Yes, strange indeed.  I'm guessing you found an undocumented-feature. smile

Last edited by bluesxman (23 May 2008 17:23)


cmd | *sh | Ruby | Chef

Offline

#3 23 May 2008 18:16

klint
Member
Registered: 19 Mar 2008
Posts: 11

Re: Has cmd.exe gone completely mad or what?

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

#4 24 May 2008 12:54

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,035
Website

Re: Has cmd.exe gone completely mad or what?

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

#5 03 Jun 2008 15:47

klint
Member
Registered: 19 Mar 2008
Posts: 11

Re: Has cmd.exe gone completely mad or what?

Simon Sheppard wrote:

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

#6 03 Jun 2008 22:39

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,035
Website

Re: Has cmd.exe gone completely mad or what?

Ah yes I see what you mean, interestingly the bug still crops up even if you specify SETLOCAL

Offline

Board footer

Powered by FluxBB