I found string functions like toUpper
for batch files here, and also on stackoverflow, but I am unable to figure out how to use them. I found several examples and instructions, some of which are contradictory, but none of them worked for me, for some reason. How would I use a function like this in a .bat file? Here is a sample code for toUpper
:
@echo off
setlocal enabledelayedexpansion
:: Main script starts here
set "arg=exampleString"
:: Output the original
echo Original: %arg%
:: Call the toUpper function to convert %arg% to uppercase
call :toUpper arg
:: Output the result
echo Uppercase: %arg%
EXIT /b 1
:toUpper str -- converts lowercase character to uppercase
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
What I get is this:
Original: exampleString
Uppercase: "ü=Ü"rg:ü=Ü
What's going on here?
EDIT: added missing return statement (see comments to answer)
nearly...
Add a goto :eof
before the subroutine, else the code will run through it unintended.
Second: you already enabled delayed expansion, so use it:
goto :eof
:toUpper str -- converts lowercase character to uppercase
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" "ä=Ä"
"ö=Ö" "ü=Ü" "ß=SS") do (
set "%~1=!%~1:%%~a!"
)
EXIT /b
The output is not quite what you want because of the Umlaute. Add a chcp 65001
at the top of your script to switch to a codepage which can handle them.
I also suggest adding "ß=SS"
for proper grammatics.