String replace with delayed expansion (case sensitive) - Batch

237 Views Asked by At

I would like to write a really simple script to "encode" and "decode" strings, by replacing each character with a number, with delayed expansion enabled. But the syntax I use should be wrong as it doesn't work at all.

Plus, is there a way to perform a case sensitive replacement, in order to restore the original string ?

@echo off
setlocal EnableDelayedExpansion

set "alphabet=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "offset=15"
set "delimiter=-"

set "string=TeSt 123 tEsT 456"

set encoded=!string!
for /l %%N in (0 1 61) do (
    set "char=!alphabet:~%%N,1!"
    set /a "code=%%N+!offset!"
    echo !char! = !code!
    set "encoded=!encoded:%char%=%code%%delimiter%!"
)

set decoded=!encoded!
for /l %%N in (0 1 61) do (
    set "char=!alphabet:~%%N,1!"
    set /a "code=%%N+!offset!"
    echo !code! = !char!
    set "decoded=!decoded:%code%%delimiter%=%char%!"
)

echo original: !string!
echo encoded: !encoded!
echo decoded: !decoded!

pause
exit

Thanks!

0

There are 0 best solutions below