Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch - Converting variable to uppercase

How would I go about changing the destl variable to uppercase before it is used. I assume some sort of character swap, however I couldn't get it working. Code is as follows -

@echo off
echo.

set /P "destf=Enter First Name: "

set /P "destl=Enter Last Name: "

set "findest=Z:\ProjectIT\copy\%destl%, %destf%"

robocopy Z:\ProjectIT\copy\test "%findest%" /e /NFL /NDL /NJH /NJS

robocopy Z:\ProjectIT\copy\Construction "%findest%"\1-BLANK-%destl% /e /NFL /NDL /NJH /NJS"

echo Construction folder has been created for "%destl%"
echo.

pause

I have tried calling something like the following, but could not get it to work -

:Uppercase
set %~1=!%1:a=A!
set %~1=!%1:b=B!
set %~1=!%1:c=C!
set %~1=!%1:d=D!
set %~1=!%1:e=E!
set %~1=!%1:f=F!
set %~1=!%1:g=G!
set %~1=!%1:h=H!
set %~1=!%1:i=I!
set %~1=!%1:j=J!
set %~1=!%1:k=K!
set %~1=!%1:l=L!
set %~1=!%1:m=M!
set %~1=!%1:n=N!
set %~1=!%1:o=O!
set %~1=!%1:p=P!
set %~1=!%1:q=Q!
set %~1=!%1:r=R!
set %~1=!%1:s=S!
set %~1=!%1:t=T!
set %~1=!%1:u=U!
set %~1=!%1:v=V!
set %~1=!%1:w=W!
set %~1=!%1:x=X!
set %~1=!%1:y=Y!
set %~1=!%1:z=Z!

Sorry about the rough code - I'm quite new to this.

Regards,

Joshua

like image 384
Joshua Patterson Avatar asked Jan 11 '16 03:01

Joshua Patterson


3 Answers

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!

like image 128
Joshua Patterson Avatar answered Oct 30 '22 07:10

Joshua Patterson


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%
like image 23
rojo Avatar answered Oct 30 '22 06:10

rojo


@ECHO Off
SETLOCAL
SET "betabet=abcdefghijklmnopqrstuvwxyz1234567890!*$^&^^+=-\^|^>;'.,/?^<"
ECHO %betabet%
>u:\betabet.file ECHO %betabet%
CALL :upper betabet
ECHO %betabet%
CALL :upcase u:\betabet.file 
GOTO :EOF

:upper
FOR %%a 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 "%1=%%%1:%%a=%%a%%%"
GOTO :EOF

:upcase
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (%1) do (
   set "line=%%a"
   for %%b 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 "line=!line:%%b=%%b!"
   )
   echo !line!
)
endlocal
GOTO :eof

The method referred to by Aacini at Windows batch file read text file and convert all to uppercase fails for some characters, as is demonstrated by the above batch.

This batch will also fail if the string in question contains certain characters (eg %:) - it will convert the alphas, but delete % and :.

The above batch first establishes a string containing miscellaneous characters, displays it, saves it as a file, then converts it using :upper.

For comparison, the file is then procesed using the :upcase function (derived from the linked response).


Following Aacini's valid comment and further investigation, here are some techniques (including a demo of the 'read-from-file' method)

The :showline routine exists to shorten the code by displaying line in delayedexpansion mode

@ECHO Off
SETLOCAL
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
SETLOCAL enabledelayedexpansion
>u:\betabet.file ECHO "!line!"
endlocal
CALL :showline
ECHO --- show conversion using "upper - caret disappears
CALL :upper line
CALL :showline
ECHO ------------------------------
ECHO --- show actual file contents ^& data read from file
type u:\betabet.file 
CALL :upcase u:\betabet.file 
ECHO ------------------------------
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
CALL :showline
ECHO --- show conversion using "inline-conversion - caret PRESERVED
setlocal ENABLEDELAYEDEXPANSION
for %%b 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 "line=!line:%%b=%%b!"
echo "!line!"   ^<--- in inline conversion
endlocal&SET "line=%line:^=^^%"
CALL :showline
ECHO ------------------------------
set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"
CALL :showline
ECHO --- show conversion using "upcase2 - caret disappears
CALL :upcase2 line
CALL :showline
GOTO :EOF

:upper
FOR %%a 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 "%1=%%%1:%%a=%%a%%%"
GOTO :EOF

:upcase
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (%1) do (
 ECHO read%%a
   set "line=%%a"
   for %%b 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 "line=!line:%%b=%%b!"
   )
   echo conv!line!
)
endlocal
GOTO :eof

:upcase2
setlocal ENABLEDELAYEDEXPANSION
for %%b 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 "%1=!%1:%%b=%%b!"
endlocal&CALL SET "%1=%%%1%%"
GOTO :eof

:showline
SETLOCAL enabledelayedexpansion
ECHO "!line!"
endlocal
GOTO :eof
like image 2
Magoo Avatar answered Oct 30 '22 06:10

Magoo