For example I have such loop that calls dir on a folder whose name contains percent signs so interpreter tries to resolve characters between these as a variable. Such folders are for example common in virtualizing solutions (for example ThinApp), that is data which would be stored in local user AppData is instead written to for example: X:\My Virtualized App\%AppData%.
And of course I know that it is possible to dir through it by doubling the %'s but it is not possible to convince interpreter to not resolve such variable in a for loop, for example:
FOR /F "tokens=*" %%F IN ('dir /b /s X:\myapp\%AppData% ') DO @(
echo %%F
)
Here no matter what I tried , doubling, quadrupling percents, or adding carets made no difference. The path passed to dir command has resolved appdata and thus is invalid having two drive specifications.
The percent sign (%) is a special case. On the command line, it does not need quoting or escaping unless two of them are used to indicate a variable, such as %OS%. But in a batch file, you have to use a double percent sign (%%) to yield a single percent sign (%).
In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ). That way, a single percent sign will be used as literal within the command line, instead of being further interpreted.
Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.
@ECHO OFF &SETLOCAL
FOR /F "delims=" %%F IN ('echo X:\myapp\%AppData%') DO (
echo %%F
)
FOR /F "delims=" %%F IN ('echo X:\myapp\^^%%AppData^^%%') DO (
echo %%F
)
FOR /F "delims=" %%F IN ('echo "X:\myapp\^^%%AppData^^%%"') DO (
echo %%F
)
FOR /F "delims=" %%F IN ('echo ^^"X:\myapp\^%%AppData^%%^"') DO (
echo %%F
)
Output:
X:\myapp\C:\Users\User\AppData\Roaming X:\myapp\%AppData% "X:\myapp\^^%AppData^^%" "X:\myapp\%AppData%"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With