aGerman wrote:As you can see assigning a variable is safe with delayed expansion switched off while you can safely work with the variable content if delayed expansion is enabled.
Thank you for that inspiring example. Manipulating the elusive exclamation (!) mark using just batch has baffled me for some time now. I followed your model and , when it worked, I incorporated some more general poison character handling. So here is a test file:
Code: Select all
batch
is
dumb!
=%"^
Very dumb, indeed!
!
The teacher angry: Shut up! Be quiet and listen to me!
<>&|
The=teacher%angry:"Shut up!^Be<quiet>and&listen|to me!
Here is the display when naming the file as the first argument to the batch script:
Code: Select all
C:\Users\Zani\Scripts>poisoner poisoned.txt
batch
is
awesome.
Very awesome, indeed.
.
The teacher angry: Shut up. Be quiet and listen to me.
The teacher angry: Shut up. Be quiet and listen to me.
The code was set to replace '!' with '.' and the other poison characters to <blank>. I did not add the file creation and rename in this example.
Code: Select all
@Echo Off
:: The first argument is the input filename
SETLOCAL Enabledelayedexpansion
set "DelChars== %% ^" ^^^^ ^^! ^< ^> ^& ^| "
set "DelChars=!DelChars: =!"
set XclChar=^^!
set "XclNewChar=."
set "AnyNewChar= "
SETLOCAL Disabledelayedexpansion
For /F "usebackq tokens=* delims=" %%I in ("%~1") DO (
set "str=%%I[[EoS]]"
SETLOCAL Enabledelayedexpansion
For /L %%A in (32767,-128,0) do (
set Tstr=!str:~%%A!
If /I "!Tstr!" EQU "" Set LastEmpty=%%A
)
For /L %%A in (!LastEmpty!,-1,0) do (
set Tstr=!str:~%%A,7!
If /I "!Tstr!" EQU "[[EoS]]" Set EndTag=%%A
)
Set /A EndStr=EndTag-1
For /L %%A in (!EndStr!,-1,0) do (
set Tstr=!str:~%%A,1!
If /I "!Tstr!" EQU "!XclChar!" (
set NEWstr=%xclNewChar%!NEWstr!
) Else (
set NEWstr=!Tstr!!NEWstr!
)
)
Rem. Echo.!NEWstr!
Set "PoisonString=!NEWstr!"
For /L %%C in (0,1,8) do (
Set "Base=!DelChars:~%%C,1!"
For /L %%P in (!EndStr!,-1,0) do (
Set "Test=!PoisonString:~%%P,1!"
If /I "!Test!" EQU "!Base!" (
Set "Sterile=%AnyNewChar%!Sterile!"
) Else (
Set "Sterile=!PoisonString:~%%P,1!!Sterile!"
)
)
Set "PoisonString=!Sterile!"
Set "Sterile="
)
set PoisonString=!PoisonString:dumb=awesome!
Echo.!PoisonString!
EndLOCAL
)
Exit /B
Of course, it is more expeditious to use hybrid scripts but I just had to try this as a batch script. Thank you again, aGerman, for directing me to a method.
John A.