Earlier today I posted a reply to an old thread initiated by dbenham on StackOverflow.
The small batch script I posted contains lots of comments.
The bizarre problem I have is that if the FIRST comment in the for loop contains a ':' anywhere, then the script fails when invoked with the "\" argument.
Code: Select all
#2 E:on V:off C:\JFL\Temp> t \progra~1\window~1 C:\Program Files\Windows Defender #2 E:on V:off C:\JFL\Temp> t \ & was unexpected at this time. #2 E:on V:off C:\JFL\Temp>
If I use :: instead of :#, the problem persists.
If I remove ALL comments except the first two in the for loop, the problem persists.
And if I change both to rem instead of :# or ::, the problem persists if there's a ':' anywhere within the first comment!
For example this modified script reproduces the problem: (Notice the word "sh:rt" instead of "short" in the first comment.)
Code: Select all
@echo off :GetLongPathname %1=PATHNAME %2=Output variable name setlocal EnableDelayedExpansion set "FULL_SHORT=%~fs1" set "FULL_SHORT=%FULL_SHORT:~3%" set "FULL_LONG=%~d1" echo set "FULL_SHORT=!FULL_SHORT!" if defined FULL_SHORT ( for %%x in ("%FULL_SHORT:\=" "%") do ( rem Loop on all sh:rt components. set "ATTRIB_OUTPUT=" & rem for /f "delims=" %%l in ('attrib "!FULL_LONG!\%%~x" 2^>NUL ^| findstr /v /c:" - %~d1"') do set "ATTRIB_OUTPUT=%%l" if defined ATTRIB_OUTPUT ( for %%f in ("!ATTRIB_OUTPUT:*\=\!") do set "LONG_NAME=%%~nxf" ) else ( set "LONG_NAME=%%~x" ) set "FULL_LONG=!FULL_LONG!\!LONG_NAME!" ) ) else ( set "FULL_LONG=%~d1\" ) endlocal & if not "%~2"=="" (set "%~2=%FULL_LONG%") else echo %FULL_LONG% exit /b
Obviously this is linked to the %FULL_SHORT:\=" "% variable expansion when that variable is empty.
But I don't understand the relationship with a ':' anywhere in the following comment!
Anybody has clues?