Batch - Converting variable to uppercase
The shortest way (without requiring 3rd party downloads) would be to use PowerShell.
set "str=The quick brown fox"
for /f "usebackq delims=" %%I in (`powershell "\"%str%\".toUpper()"`) do set "upper=%%~I"
A faster way but still using less code than any pure batch solution would be to employ WSH.
@if (@CodeSection == @Batch) @then
@echo off & setlocal
set "str=The quick brown fox"
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%str%"') do set "upper=%%~I"
set upper
goto :EOF
@end // end Batch / begin JScript hybrid
WSH.Echo(WSH.Arguments(0).toUpperCase());
And of course, you can easily make either a function so you can call
it multiple times as needed.
@if (@CodeSection == @Batch) @then
@echo off & setlocal
call :toUpper upper1 "The quick brown fox"
call :toUpper upper2 "jumps over the lazy dog."
set upper
goto :EOF
:toUpper <return_var> <str>
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0" "%~2"') do set "%~1=%%~I"
goto :EOF
@end // end Batch / begin JScript hybrid
WSH.Echo(WSH.Arguments(0).toUpperCase());
Or if you want to be really hacksy about it, you could abuse the tree
command's error message like this:
@echo off & setlocal
set upper=
set "str=Make me all uppercase!"
for /f "skip=2 delims=" %%I in ('tree "\%str%"') do if not defined upper set "upper=%%~I"
set "upper=%upper:~3%"
echo %upper%
Thanks for the responses guys, I solved it using this -
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
I'm sure your responses are far neater and more efficient, but as mine is doing the trick and I don't want to break anything I will leave it as is!
Thank you for your input!