Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make variable all lower case in batch without call

Here is a code I found to make a value lower case, but was curious how to do it without the CALL and just use a variable:

SET String=Hello, how are you ?
CALL :LoCase String
:LoCase
FOR %%i IN ("A=a" "B=b" "C=c" "D=d" "E=e" "F=f" "G=g" "H=h" "I=i" "J=j" "K=k" "L=l" "M=m" "N=n" "O=o" "P=p" "Q=q" "R=r" "S=s" "T=t" "U=u" "V=v" "W=w" "X=x" "Y=y" "Z=z") DO CALL SET "%1=%%%1:%%~i%%"
GOTO:EOF
like image 758
epickmatt Avatar asked Jul 15 '26 02:07

epickmatt


1 Answers

I'm not sure which call you want to avoid - the one in the for loop or calling the subroutine. But it is possible to ditch both with macro and delayedExpansion which will be executed much faster:

@echo off

set locase=for /L %%n in (1 1 2) do if %%n==2 ( for %%# in (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) do set "result=!result:%%#=%%#!") ELSE setlocal enableDelayedExpansion ^& set result=

set "string=SOme STrinG "
%locase%%string%

echo %result%

Also because of the way how replacement works in batch files you can just list the lower case letters - this will increase the performance also. Probably the code above is the fastest possible way to set string in lowercase only in batch file.

like image 153
npocmaka Avatar answered Jul 18 '26 04:07

npocmaka