Destructive env var substring
Posted: 18 Feb 2013 04:36
Code: Select all
@echo off
echo %%cmdcmdline%%=%cmdcmdline%
echo %%cmdcmdline:~0,6%%=%cmdcmdline:~0,6%
echo %%cmdcmdline%%=%cmdcmdline%
echo;
echo %%temp%%=%temp%
echo %%temp:~0,6%%=%temp:~0,6%
echo %%temp%%=%temp%
echo;
echo %%cd%%=%cd%
echo %%cd:~0,6%%=%cd:~0,6%
echo %%cd%%=%cd%
echo;
pause
cls
outputs:
Code: Select all
%cmdcmdline%=cmd /c ""C:\Documents and Settings\Administrator\Desktop\test19.bat" "
%cmdcmdline:~0,6%=cmd /c
%cmdcmdline%=cmd /c
%temp%=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
%temp:~0,6%=C:\DOC
%temp%=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
%cd%=C:\Documents and Settings\Administrator\Desktop
%cd:~0,6%=C:\Doc
%cd%=C:\Documents and Settings\Administrator\Desktop
Press any key to continue . . .
That third line, %cmdcmdline%=cmd /c, what madness is that?! This was on XP SP3. I was experimenting with cmdcmdline, considering ways to check it to determine the current execution state, when I noticed doing a substring on it permanently altered it; the clipped result propagated to child batch instances.
Obviously there are workarounds, but I don't suppose there's a technical explanation for why that's occurring, is there?
Edit: Not surprisingly, string replacements also permanently alter it.
Queue