Functional carriage return literal within a batch script!
Posted: 27 Mar 2015 13:02
Normally the only way to work with carriage return characters within batch is via FOR variables or delayed expansion. This is because the batch parser strips all carriage returns after the normal expansion phase, as documented in phase 1.5 in jeb's StackOverflow post - http://stackoverflow.com/a/4095133/1012053
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
I then used my JREPL.BAT utility to transform \r into a carriage return literlal:
Here is what the final code looks like (note the appearance of the last line):
And here is the output:
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
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