Replace file skips ! character

Microsoft Windows xp pro w/ sp3 multilin...
January 22, 2010 at 04:56:40
Specs: Windows XP
Hi Everybody.
I have a replace text in a file batch that works OK.
Only problem is that I'm parsinh .html files and whenever the Batch files finds and HTML comment like this: <!-- whatever --> It skips the ! and ommit it in the replaced string, resulting a: <-- whatever --> As there's no exclamation mark in the opening tag the browser ignores it's a comment and paint it in the screen.

How can I solve this?
Thanks in advance.
Script is as follows:
@echo off
@echo off > newfile & setLocal enableDELAYedexpansion

for %%X in (*.html) do set archivo=%%X


set /p old=escriba el idpartner ANTIGUO y pulse Enter ?
set /p new=escriba el idpartner NUEVO y pulse Enter ?

for /f "tokens=* delims= " %%a in (%archivo%) do (
set str=%%a
set str=!str:%old%=%new%!
>> letter_nueva.html echo !str!
)
del newfile*.*
pause


See More: Replace file skips ! character

Report •

#1
January 22, 2010 at 05:20:33
Try this:

<untested>

@echo off > newfile & setLocal disableDELAYedexpansion

for %%X in (*.html) do set archivo=%%X


set /p old=escriba el idpartner ANTIGUO y pulse Enter ?
set /p new=escriba el idpartner NUEVO y pulse Enter ?

for /f "tokens=* delims= " %%a in (%archivo%) do (
set str=%%a
SetlocalEnableDelayedExpansion
set str=!str:%old%=%new%!
>> letter_nueva.html echo !str!
endlocal
)
del newfile*.*

pause


Batch Variable how to


Report •

#2
January 25, 2010 at 00:50:20
I'm afraid it didn't work.
I tried with a slight modification:
setlocal enableDelayedexpansion
instead of:
SetlocalEnableDelayedExpansion

but still the same.
Any idea?


Report •

#3
January 25, 2010 at 03:05:09
not tested (but some variation should work...)
echo off > newfile & setLocal enableDELAYedexpansion
for %%X in (*.html) do set archivo=%%X
set /p old=escriba el idpartner ANTIGUO y pulse Enter ?
set /p new=escriba el idpartner NUEVO y pulse Enter ?
for /f "tokens=* delims= " %%a in (%archivo%) do (
set str=%%a
setlocal disabeldelayedexpansion
set str=%old:!=+++%
set str=%new:!=+++%
endlocal enabledelayedexpansion
set str=!str:%old%=%new%!
setlocal disabeldelayedexpansion
set str=%str:+++=!%
endlocal enabledelayedexpansion
>> letter_nueva.html echo !str!
)

ps: i'm going to test it later 2day, too tired rt.now.


Report •

Related Solutions

#4
January 25, 2010 at 03:28:01
Does this have to be plain batch?

If a third party utility is an option it would be so much easier.

I tend to use a utility called sfk(click).

It would make things as simple as this:

@ECHO OFF
setlocal EnableDelayedExpansion
set /p old=escriba el idpartner ANTIGUO y pulse Enter ?
set /p new=escriba el idpartner NUEVO y pulse Enter ?
if defined new set new=!new:\=\x5c!
if defined old set old=!old:\=\x5c!
if defined new set new=!new:/=\x2f!
if defined old set old=!old:/=\x2f!
if defined new set new=!new:"=\q!
if defined old set old=!old:"=\q!
sfk rep "blah.txt" "/!old!/!new!/" -yes -spat
pause

===========================================

If this must be batch alone it may still be doable, but we will need some more information.

What type of things are you using as old and new, if you expect to be able to replace anything with anything I would say it's just not feasible(it may not even be possible!) in batch alone.


Batch Variable how to


Report •

#5
January 25, 2010 at 05:03:04
Hi. It should be plain Batch.

old and new take as input only ASCII characters as:
whatever12345_andsomethingelse (no strange characters...)

The original batch works OK excepts that parsing <!--- comment --> results in <-- comment parsed --> (without the !)


Report •

#6
January 25, 2010 at 05:07:47
@Judago,
amen to that! batch is sometimes really great, sometimes batch is bitch.
I favor just plain old QB or edlin for these kinds of operations, but i always got the impression "third party" utils were frowned upon, so i've always stuck to batch.
QB could be hacked out in about 2 minutes (less coffee time), edlin would work but for the ctrl-f_ing Z it insists on using between strings (on Replace ops). gnu utilities I'm sure would work but i get headaches working with RE's..
I almost had a funky batch working for this, but gave up on it due to lack of sleep.

ps crossed up with OP's last resp. It can be done in batch, i'm sure.

funky batch attack:
@echo off & setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%z in (xyz) do (
setlocal disabledelayedexpansion
set test="%%z"
echo.z:"%%z"
call :uu
endlocal enabledelayedexpansion
)
goto :eof

:uu
echo.INPUT-UU:[%test%]
set test=%test:!=+++%
echo EXCLAMS REMOVED:[%test%]
set /p xx=from:
set /p yy=to:
setlocal enabledelayedexpansion
set test=!test:%xx%=%yy%!
echo....[!test!]
setlocal disabledelayedexpansion
set test=%test:+++=!%
setlocal enabledelayedexpansion
echo....[!test!]
for /f "tokens=* delims=" %%y in ("!test!") do set xx=%%~y
set test=!test:~1,-1!
echo.VV:OUTPUT:[!test!]
pause
)
goto :eof

very crude, prob'ly will generate setlocal recursion error used on a real file.


Report •

#7
January 25, 2010 at 06:16:24
@barleby

Are you trying to replace !'s as either the old or new string? If so it's not going to happen easily.....

What I posted in #1 works fine for me with the missing space you pointed out and adding my own file name(i.e !'s were kept), so long as I don't try to replace any !'s. I don't know what to say, it works for me.....

There is another method but other chars become problems(especially double quotes).

@nbrane

"I favor just plain old QB or edlin for these kinds of operations, but i always got the impression "third party" utils were frowned upon, so i've always stuck to batch."

Yep, batch goes both ways, it can be easy and useful or painful compared to other tools...

Third party utilities are basically frowned upon but I don't see any harm in suggesting them(without pushing the issue). The main reason I suggested something is that I actually sat down for like 4 hours trying to write a script that could replace any printable ascii string with any other ascii string and failed miserably. I have yet to see any such script anywhere.....


Batch Variable how to


Report •

#8
January 25, 2010 at 07:10:07
Judago, you're right #1 with the missing space works Well. I made an error when testing with your example.
Thanks a lot to everybody, you all were very helpful :)

Report •

#9
January 25, 2010 at 07:15:08
This is a lot of chatter for a simple problem. This line:
@echo off > newfile & setLocal enableDELAYedexpansion
Disables the usage of the exclamation mark. If you require delayed expansion, you cannot use '!'. They are mutually exclusive.

I suggest you use Window's native scripting language, VBScript.


Report •

#10
January 26, 2010 at 00:24:12
Hi R2,

Yeah, I dunno vbs. Working htnl with bat has plenty of problems. Not least the ! and others.

This script creates a small .com util and does the changes. It will work on files up to about 30KB. For anything bigger we'll need the heavy artillary.

=================================

@echo off & setLocal enableDELAYedeXpansion

if not exist change.com call :make change

for /f "tokens=* delims= " %%a in ('dir /b *.html') do (
set /p old=escriba el idpartner ANTIGUO y pulse Enter ?
set /p new=escriba el idpartner NUEVO y pulse Enter ?
move /y "%%a" #
change.com # "!old!" "!new!"
echo.
move /y # "%%a"
)

goto :eof

:make change

@echo off > d.d
>> d.d echo E0100 E9 86 00 00 00 00 00 00 00 00 53 79 6E 74 61 78 
>> d.d echo E0110 20 65 72 72 6F 72 24 46 69 6C 65 20 6E 6F 74 20 
>> d.d echo E0120 66 6F 75 6E 64 24 46 69 6C 65 20 74 6F 6F 20 62 
>> d.d echo E0130 69 67 24 4E 6F 74 20 43 68 61 6E 67 65 64 24 43 
>> d.d echo E0140 6F 70 79 72 69 67 68 74 20 31 39 38 36 20 5A 69 
>> d.d echo E0150 66 66 2D 44 61 76 69 73 20 50 75 62 6C 69 73 68 
>> d.d echo E0160 69 6E 67 20 43 6F 2E 1A 50 72 6F 67 72 61 6D 6D 
>> d.d echo E0170 65 64 20 62 79 20 4D 69 63 68 61 65 6C 20 4A 2E 
>> d.d echo E0180 20 4D 65 66 66 6F 72 64 1A BE 80 00 BA 0A 01 FC 
>> d.d echo E0190 E8 35 01 89 36 03 01 80 3C 0D 74 3F 80 3C 20 74 
>> d.d echo E01A0 03 46 EB F3 C6 04 00 E8 1E 01 33 C9 BF E1 02 E8 
>> d.d echo E01B0 D7 00 80 3C 20 74 08 80 3C 0D 74 1F 46 EB F0 E8 
>> d.d echo E01C0 06 01 89 0E 05 01 33 C9 BF 61 03 E8 BB 00 80 3C 
>> d.d echo E01D0 20 74 0E 80 3C 0D 74 09 46 EB F0 B4 09 CD 21 CD 
>> d.d echo E01E0 20 89 0E 07 01 8B 16 03 01 B8 00 3D CD 21 BA 17 
>> d.d echo E01F0 01 72 E8 8B D8 BA 01 52 B9 40 9C B4 3F CD 21 BA 
>> d.d echo E0200 26 01 3D 40 9C 74 D4 50 B4 3E CD 21 5A A1 05 01 
>> d.d echo E0210 3B D0 7D 05 BA 33 01 EB C2 BB 01 52 B8 E1 03 BE 
>> d.d echo E0220 E1 02 8B FB 8B 0E 05 01 F3 A6 8B F8 74 08 8B F3 
>> d.d echo E0230 A4 40 43 4A EB 18 80 0E 09 01 01 BE 61 03 8B 0E 
>> d.d echo E0240 07 01 03 C1 03 1E 05 01 2B 16 05 01 F3 A4 3B 16 
>> d.d echo E0250 05 01 7D CB 8B CA 03 C1 8B F3 F3 A4 50 8B 16 03 
>> d.d echo E0260 01 33 C9 B4 3C CD 21 59 81 E9 E1 03 8B D8 BA E1 
>> d.d echo E0270 03 B4 40 CD 21 B4 3E CD 21 BA 33 01 80 3E 09 01 
>> d.d echo E0280 00 74 03 83 C2 04 E9 52 FF 80 3C 22 75 14 46 AC 
>> d.d echo E0290 3C 0D 74 31 3C 22 75 06 E8 34 00 73 01 C3 AA 41 
>> d.d echo E02A0 EB ED 32 DB E8 28 00 73 05 8A C3 AA 41 C3 AC 3C 
>> d.d echo E02B0 30 72 12 3C 39 77 0E 2C 30 8A F8 B0 0A F6 E3 8A 
>> d.d echo E02C0 D8 02 DF EB DF E9 13 FF 46 80 3C 20 74 FA C3 F8 
>> d.d echo E02D0 80 3C 20 74 0A 80 3C 0D 74 05 80 3C 2C 75 01 F9 
>> d.d echo E02E0 C3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E02F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0310 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0320 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0330 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0340 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0350 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0370 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0380 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E0390 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E03A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E03B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E03C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E03D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
>> d.d echo E03E0 00 
>> d.d echo N change.com
>> d.d echo RCX
>> d.d echo 02E1
>> d.d echo W
>> d.d echo Q
debug < d.d > nul
del d.d

goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question