Exclamation Points Disappearing

March 16, 2009 at 15:55:03
Specs: Win XP, Pent 4 / 1 GB
I am formatting basic text files with HTML but when I run my batch file on them all the exclamation points seem to disappear. Is there any way to tell the process that the ex-points are text characters and not pieces of code?

Here is my beginning code and it zaps the exclamation points so that none appear in temp2.txt

:: --- set <br> for empty spaces ---
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^<temp1.txt') do (
set str=%%b
if "%%b"=="" set str=!str:^^%%b^<br^>!
echo.!str!>> temp2.txt
)

Jesus is the Rock - ROCK ON!


See More: Exclamation Points Disappearing

Report •


#1
March 17, 2009 at 00:33:58
Because delayed expansion causes exclamation marks to flag variables they, as you mention, disappear.

Unfortunately their are only two ways (that I am aware of) to keep the exclamation marks to stay untouched, either escape them with 2 ^^carats or not have delayed expansion enabled.

With the for loop over a file you have no chance to apply carats but there is a trick that can be used, if you set a variable with text that contains an exclamation mark before you enable delayed expansion the exclamation mark inside the variable will not expand/disappear.

example:

rem delayed expansion must not be enabled at this point
for /f %%a in (test.txt) do (
set var=%%a
SETLOCAL ENABLEDELAYEDEXPANSION
echo !var!
pause
endlocal
)

Just remember the endlocal at the end, with out it you will probably get setlocal recursions.

If you have a variable that you must keep afterwards(because endlocal should kill everything set since setlocal), this may help.

for /f "delims=" %%z in ("!var!") do endlocal&set var=%%z


Report •

#2
March 17, 2009 at 01:40:43
nice one, now i understand about the setlocal and endlocal inside for loop.

Report •

#3
March 18, 2009 at 09:54:28
Works great! Thanks Judago.

Here's a chunk that I am now successfully running:

:: --- set <br> for empty spaces ---
for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^<temp1.txt') do (
set var=%%a
set str=%%b
setlocal enabledelayedexpansion
if "%%b"=="" set str=!str:^^%%b^<br^>!
echo !str! >> temp2.txt
endlocal
)

del temp1.txt
rename temp2.txt temp1.txt

Jesus is the Rock - ROCK ON!


Report •

Related Solutions


Ask Question