Skip navigation

JSI Tip 8971. How can I change the case of string to all upper case or all lower case?


In tip 6301, I determined if a string is all alphabetic or all numeric. In tip 6302, I determined if an all alphabetic string is all upper case or all lower case.

NOTE: See Another way to change the case of string to all upper case or all lower case?

I have scripted ChgCase.bat to change a string to all upper case or all lower case.

The syntax for using ChgCase.bat is:

call ChgCase U|L VarName

Where:

U        changes the string in VarName to upper case.
L        changes the string in VarName to lower case.

VarName  is the name of the variable that contains the string to be converted.

Sample Usage:

Update all users displayName attributes so that the first character in each name segment is upper case and the remaining characters are lower case:
@echo off
setlocal
set filter="(&(objectCategory=Person)(objectClass=User))"
set qry=DSQUERY * domainroot -filter %filter% -attr distinguishedName displayName -L -limit 0
for /f "Tokens=1*" %%a in ('%qry%') do (
 set attr=%%a
 set data=%%b
 call :doit1
)
endlocal
goto :EOF
:doit1
if "%attr%" EQU "distinguishedName:" set userDN=%data%&goto :EOF
if "%data%" EQU "" goto :EOF
call echo %data%|FIND "=">nul
if %ERRORLEVEL% EQU 0 goto :EOF
call :doit2 %data%
if "%data%" EQU "%name:~1%" goto :EOF
DSMOD user "%userDN%" -Display "%name:~1%"
goto :EOF
:doit2
set name=
:doit3
if \{%1\}

\{\} goto :EOF set s=%1 set v1=%s:~0,1% set vx=%s:~1% call ChgCase U v1 call ChgCase L vx set name=%name% %v1%%vx% shift goto doit3

ChgCase.bat contains:
@echo off
If \{%2\}\{\} goto :err
:: Test for nul value and exit
for /f "Tokens=*" %%c in ('@echo %%%2%%') do if \{%%c\}

\{%%%2%%\} goto :EOF If /i \{%1\}

\{U\} goto UCase If /i \{%1\}==\{L\} goto LCase :err @echo Syntax: ChgCase U^|L VarName goto :EOF :UCase for %%c 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 call set %2=%%%2:%%c=%%c%% goto :EOF :LCase for %%c 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 call set %2=%%%2:%%c=%%c%%
uppercase lowercase



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish