Dostips :toupper case and toLower case

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
billrich
Posts: 70
Joined: 24 Apr 2012 05:36
Location: USA

Dostips :toupper case and toLower case

#1 Post by billrich » 19 Nov 2012 17:49

Call set %~1=%%%~1:%%~a%%
(Please explain how the above line works.)

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET String=%1
Set Case=%2
if !Case!==low  goto :lo
goto :hi
rem SET String
:hi
echo in hi
CALL :toUpper String
SET String
goto :EOF
SET String
:lo
echo in lo
CALL :toLower String
SET String
ENDLOCAL
rem GOTO:EOF
exit /b


:toUpper
rem Description: call:toUpper str

 :toUpper str -- converts lowercase character to uppercase
::           -- str [in,out] - valref of string variable to be converted
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source http://www.dostips.com
if not defined %~1 EXIT /b
for %%a 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:%%~a%%
)
EXIT /b

rem :toLower - converts uppercase character to lowercase
rem Description: call:toLower str
:toLower str -- converts uppercase character to lowercase
::           -- str [in,out] - valref of string variable to be converted
:$created 20060101 :$changed 20080219 :$categories StringManipulation
:$source http://www.dostips.com
if not defined %~1 EXIT /b
for %%a 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:%%~a%%
)
EXIT /b


Output:

C:\test>tipsUL.bat BILL.TXT low
in lo
String=bill.txt

C:\test>tipsUL.bat bill.txt hi
in hi
String=BILL.TXT

C:\test>tipsUL.bat abc hi
in hi
String=ABC


C:\test>echo ABC | awk {'print tolower($_)'}
abc


C:\test>echo abc | awk {'print toupper($_)'}
ABC
Last edited by billrich on 20 Nov 2012 11:46, edited 3 times in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Dostips :toupper case and toLower case

#2 Post by dbenham » 19 Nov 2012 20:19

That line is in a loop with 29 iterations, 1 for each letter to be capitalized.

%1 = String - the name of the variable that contains the string to be captialized.
In the first iteration the %%a has a value of "a=A"
Let us assume that the value of String is "abc"

The CALL statement provides an extra level of variable expansion which is critical to how the function works. The following is an attempt to explain the various phases of how the line gets parsed. It is not really individual lines of code.

Original line

Code: Select all

call set %~1=%%%~1:%%~a%%

after initial percent expansion: %~1 is expanded to String and %% becomes %

Code: Select all

call set String=%String:%~a%

after FOR variable expansion: %~a becomes a=A

Code: Select all

call set String=%String:a=A%

after CALL is parsed: The value of String is expanded and all a characters are converted to A

Code: Select all

set String=Abc


For a more in depth explanation of the batch parser, see jeb's StackOverflow answer to How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Dave Benham

Post Reply