Cristopher
Cristopher

Reputation: 11

Batch: turning only uppercase letters into symbols

I have a variable and im trying to only turn the uppercase letters into different numbers.

set var1=HeLlO
set var1=%var1:A=0%
set var1=%var1:B=1%
set var1=%var1:C=2%
set var1=%var1:D=3%
...
echo %var1%

but what is happening is when i want it to only turn the uppercase L it turns both l's into a number.

set var1=%var1:A=1%

that code turns all lowercase as and all uppercase As into 1, but i want it to only turn uppercase As into 1s

Upvotes: 1

Views: 57

Answers (1)

Squashman
Squashman

Reputation: 14320

I took this code from dbenham's ROT13. Just had to comment out a few things.

@echo off

set var=AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz!
echo var is: %var%
call :rot13 var newvar
setlocal enabledelayedexpansion
echo var is: !newvar!

pause
goto :EOF

:rot13   StrVar [RtnVar]           -- Applies the ROT13 cipher to a string
::
::  Applies the simple "rotate alphabet 13 places" cipher to the string
::  contained within variable StrVar.
::
::  Sets RtnVar=result
::  or displays result if RtnVar not specified
::
  setlocal
  set "NotDelayedFlag=!"
  setlocal enableDelayedExpansion
REM set "upper=#AAN#BBO#CCP#DDQ#EER#FFS#GGT#HHU#IIV#JJW#KKX#LLY#MMZ#NNA#OOB#PPC#QQD#RRE#SSF#TTG#UUH#VVI#WWJ#XXK#YYL#ZZM#"
    set "upper=#AA@#BB$#CC-#DD_#EE=#FF+#GG`#HH:#II;#JJ'#KK{#LL}#MM[#NN]#OO\#PP/#QQ,#RR.#SS~#TTÄ#UU?#VV<#WW>#XX|#YYÖ#ZZÜ#"
REM set "lower=#aan#bbo#ccp#ddq#eer#ffs#ggt#hhu#iiv#jjw#kkx#lly#mmz#nna#oob#ppc#qqd#rre#ssf#ttg#uuh#vvi#wwj#xxk#yyl#zzm#"

  set "str=A!%~1!"
  set "len=0"
  for /L %%A in (12,-1,0) do (
    set /a "len|=1<<%%A"
    for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
  )
  set /a len-=1
  set rtn=
  for /l %%n in (0,1,!len!) do (
    set "c=!%~1:~%%n,1!"
    if "!c!" geq "a" if "!c!" leq "Z" (
      for /f "delims=" %%c in ("!c!") do (
        set "test=!upper:*#%%c=!"
        if "!test:~0,1!"=="!c!" (
          set c=!test:~1,1!
REM         ) else (
REM          set "test=!lower:*#%%c=!"
REM          if "!test:~0,1!"=="!c!" set c=!test:~1,1!
        )
      )
    )
    set "rtn=!rtn!!c!"
  )
  if "%~2"=="" (
    echo:!rtn!
    exit /b
  )
  :: The remainder is "Jeb magic" used to transfer the rtn value across the function endlocal barrier
  if defined rtn (
    set "rtn=!rtn:%%=%%~A!"
    set "rtn=!rtn:"=%%~B!"
    if not defined NotDelayedFlag set "rtn=!rtn:^=^^^^!"
  )
  if defined rtn if not defined NotDelayedFlag set "rtn=%rtn:!=^^^!%" !
  set "replace=%% """"
  for /f "tokens=1,2" %%A in ("!replace!") do (
    endlocal
    endlocal
    set "%~2=%rtn%" !
  )
exit /b

And here is the output.

var is: AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz!
var is: @a$b-c_d=e+f`g:h;i'j{k}l[m]n\o/p,q.r~s─t?u<v>w|x╓y▄z!
Press any key to continue . . .

Upvotes: 1

Related Questions