But I was curious to see if I could demonstrate that carriage returns truly are stripped after normal expansion. So I did an experiment.
The goal is to define a variable that contains a carriage return, and then use normal expansion to expand the variable with find and replace operation that transforms the carriage return into something else. The find uses a carriage return literal within the script
Embedding a carriage return within a text file can be tricky, so I first wrote the script with the carriage return encoded as \r.
test.bat
Code: Select all
@echo off
setlocal enableDelayedExpansion
:: Define CR to contain a single carriage return character (\r).
:: This also demonstrates that \r is stripped before FOR variable expansion
for /f %%a in ('copy /Z "%~dpf0" nul') do (
set "CR=%%a"
echo FOR variable%%aOK
)
:: Normal expansion seems to fail because all carriage returns
:: are stripped from the line after expansion
echo Normal expansion%CR%FAIL
:: Delayed expansion works just fine
echo Delayed expansion!CR!OK
:: Demonstrate that Normal expansion actually works by replacing \r
:: with some other text
echo Normal expansion %CR:\r=OK%
I then used my JREPL.BAT utility to transform \r into a carriage return literlal:
Code: Select all
jrepl "\\r" "\r" /x /f test.bat /o -
Here is what the final code looks like (note the appearance of the last line):
Code: Select all
D:\test>type test.bat
@echo off
setlocal enableDelayedExpansion
:: Define CR to contain a single carriage return character.
:: This also demonstrates that carriage return is stripped before
:: FOR variable expansion
for /f %%a in ('copy /Z "%~dpf0" nul') do (
set "CR=%%a"
echo FOR variable%%aOK
)
:: Normal expansion seems to fail because all carriage returns
:: are stripped from the line after expansion
echo Normal expansion%CR%FAIL
:: Delayed expansion works just fine
echo Delayed expansion!CR!OK
:: Demonstrate that Normal expansion actually works by replacing the
:: carriage return with some other text
=OK% echo Normal expansion %CR:
And here is the output:
Code: Select all
D:\test>test
OK FOR variable
Normal expansionFAIL
OK Delayed expansion
Normal expansion OK
I don't see how this is really of any use, but I thought it was interesting. It also provides evidence to back up the phase rules developed by jeb. It is pretty amazing how jeb was able to derive all those rules, and we are all the better for it.
Dave Benham