2

After applying this SO answer on my system, I noticed that clicking on .bat files now results:

"/c" is not recognized as an internal or external command

And in an attempt to fix it I tried to replace the quotes in %CMDCMDLINE% in the IF expression using the Environment variable substitution. And then I noticed something strange. Here's my test batch file:

@echo off
echo _%CMDCMDLINE%_
echo _%CMDCMDLINE:"=_%_
echo _%CMDCMDLINE%_
pause

and this is its output:

_C:\Windows\system32\cmd.exe /c ""C:\cygwin\opt\openconnect64\rek.bat" "_
_C:\Windows\system32\cmd.exe /c __C:\cygwin\opt\openconnect64\rek.bat_ __
_C:\Windows\system32\cmd.exe /c __C:\cygwin\opt\openconnect64\rek.bat_ __

As you can see, although there's no assignment, the variable is altered. This doesn't occur for other environment variables with the same value.

basin
  • 3,949
  • 2
  • 27
  • 63
  • Instead of `IF /I x"%COMSPEC%"==x%CMDCMDLINE% (cd /D c:\)` better use `IF /I "%COMSPEC%"=="%CMDCMDLINE:"= %" (cd /D c:\)` –  Jun 22 '18 at 21:28
  • OK, I stand corrected, don't know where cmd.exe stores the automatic variables, looks like this is a pointer issue. Successive runs in an open window show the altered content, newly started cmd windows begin with double quotes. –  Jun 22 '18 at 21:45
  • 1
    basin, this site is designed to provide answers to questions with problematic code. You do not appear to have asked a question and you do not appear to have suggested that there is a problem with your code. The question is therefore either simply making a statement, which means you need a blog, or seeking reference material, which means you need a different site because requests of that type are off-topic here. Please therefore [edit the post](https://stackoverflow.com/posts/50995536/edit) to make it on-topic or delete it and find a more appropriate platform; thank you. – Compo Jun 22 '18 at 22:08

1 Answers1

3

You found the well known cmdcmdline effect.
It's the only variable that shows this very useful behaviour.

It's useful, as it's a good possibility to examine how the parser works.
Like the question, if the parser expand variables after a REM or label?
Btw. It also works the same way with delayed expansion

@echo off
echo %CMDCMDLINE%
REM %CMDCMDLINE:C:=1#%
: %CMDCMDLINE:"=X%

echo %CMDCMDLINE%

It should be avoided to make the cmdcmdline variable longer than the initial length, as this can crash cmd.exe

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 4
    When a variable read is detected by the parser the `GetEnvVar` function is called. Here a buffer is allocated to store the data returned from the `GetEnvironmentVariableW` API if the variable exists and a pointer to this buffer is returned. If the variable doesn't exist the list of dynamic vars is checked. For `CMDCMDLINE`, the `GetCommandLineW ` is called and the returned pointer to the command-line string for the current process is returned by `GetEnvVar` **instead** of the pointer to the internal buffer. As the substring/replace operations are executed inplace over pointed data, ... – MC ND Jun 23 '18 at 12:36
  • @MCND Thanks for your debug session. I made only black box testings, but it's good to known the real reason for the problem – jeb Jun 25 '18 at 08:06