12

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

alroc
  • 27,574
  • 6
  • 51
  • 97
Joshua Patterson
  • 433
  • 1
  • 4
  • 15

9 Answers9

23

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%
rojo
  • 24,000
  • 5
  • 55
  • 101
  • 3
    The conversion code originally used at [this answer](http://stackoverflow.com/questions/15648560/windows-batch-file-read-text-file-and-convert-all-to-uppercase) is this single line: `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 "str=!str:%%b=%%b!"`. I have not tested it, but is possible that this pure Batch method be as fast as the WSH one because the overhead of loading `cscript.exe` file. – Aacini Jan 11 '16 at 16:44
  • 1
    @Aacini Check out my hacksy discovery. :) – rojo Jan 11 '16 at 17:13
  • 3
    I like it very much! **`:)`** +1 It even works correctly with Spanish characters! Try: `tree "\El papá y la mamá del niño güero"` – Aacini Jan 11 '16 at 18:15
  • 2
    Woah - never thought of abusing a `tree` +1. Slightly enhanced: `for /f "skip=2 tokens=1,* delims=\" %%I .... do ... set "upper=%%J"` – Stephan Jan 12 '16 at 08:14
23

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!

Joshua Patterson
  • 433
  • 1
  • 4
  • 15
  • 1
    You found the best solution yourself! It's even neater like this: for %a in ("a=A" "b=B" "c=C") do set x=!x:%~a! – Henrik Jan 12 '16 at 08:53
  • 1
    Agreed this is by far the best solution for cmd.exe (not PowerShell) and should be at the top; it is the smallest, least-ugly hack proposed and doesn't even require "enabledelayedexpansion". NOTE: The syntax for modifying a variable instead of a command line argument is slightly different. I used the following: (call set SOME_VARIABLE=%%SOME_VARIABLE:%%~a%%) – byteptr Jul 29 '18 at 19:42
  • This is so good, thank you. I hope you don't mind, I made it into a function call in this thread. – Ste Jan 24 '20 at 19:06
2
@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
Community
  • 1
  • 1
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    The problems you reported are not related to the uppercase conversion method, but to reading lines from file. Try this: `set "line=abcdefghijklmnopqrstuvwxyz1234567890|!#$%%&/\()=?<>,;.:"'_-+*~^^[]{}"` & `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 this example all characters are correctly converted, even `%:*~!` – Aacini Jan 11 '16 at 06:52
  • **1.** This question is about "Converting **variable** to uppercase", specifically "the **destl** variable", so all your code and explanations about "read data from a file" is pointless here. You should posted this answer in the other question, that is about "**read text file** and convert all to uppercase". **2.** Your edit just confirms that the answer posted at the given link is a working solution for this problem, so it would be better to preserve the "duplicated question" mark I inserted (that you removed in order to post this answer). – Aacini Jan 11 '16 at 14:17
2

This is pointless since env var names and file names are both case insensitive. But if you insist you can do it like this:

@echo off

setlocal enabledelayedexpansion
set lower=john smith 123
set alpha=ABCDEFGHIJKLMNOPQRSTUVWXYZ
set i=0
:letter
set !alpha:~%i%,1!_=!alpha:~%i%,1!
set /a i += 1
if %i% LSS 26 goto letter
set i=0
set upper=
:uppercase
set let=!lower:~%i%,1!
if "%let%" == "" goto done
if defined %let%_ (set upper=%upper%!%let%_!) else (set upper=%upper%%let%)
set /a i += 1
goto uppercase
:done
echo %upper%
Henrik
  • 332
  • 3
  • 12
1

A general purpose translator which seems simple and able to do the job is -

@echo off
call:@translate WhIiPpiTy abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
echo %_answer%
call:@translate "12:37 am" apm APM
echo %_answer%
pause
exit  /b
:@translate
setlocal
set j=%~1
set f=%~2
set t=%~3
set x=0
:translate_loop
call set r=%%f:~%x%,1%%=%%t:~%x%,1%%
call set j=%%j:%r%%%  >NUL 2>&1
if %ERRORLEVEL%==1 goto translate_end
set /a x+=1
goto translate_loop
:translate_end
endlocal & set _answer=%j%
exit /b
Forest
  • 11
  • 2
0

I used 2 files, in the batch I called an external js and in the bat I assign the response to an internal var:

Bat:

@echo off
for /f %%i in ('cscript //nologo //E:jscript jsfile.js %1') do set VAR=%%i
echo %Var%

Js (jsfile.js in same directory):

WScript.echo(WSH.Arguments(0).toUpperCase())
cbeltrangomez
  • 412
  • 2
  • 9
0

If you have any POSIX utilities installed on Windows, like many developers (e.g., GNUWin utilities at https://sourceforge.net/projects/gnuwin32/)

You could use the tr.exe utility, which has: [:upper:]

@echo off
SETLOCAL EnableDelayedExpansion
echo Before: SCEINST is: %SCEINST%
set upper=
for /f "usebackq delims=" %%g in (`echo %SCEINST% ^| tr [:lower:] [:upper:]`) do (
    SET upper=%%g
    :: get rid of any leading whitespace
    set upper=!upper: =!
    goto :done
)
:done
echo After: SCEINST (in upper) is now: !upper!

----------OUTPUT----------

Before: SCEINST is: scprd
After: SCEINST (in upper) is now: SCPRD
Darrin
  • 317
  • 1
  • 4
  • 10
0

I've expanded upon the great answer by the OP and made it into a function that can be called throughout the batch file.

@echo off

set "str=Make meeeee all uppercaseeeee!"
echo %str% (Old variable)
call :TOUPPERCASE str
echo %str%
goto :eof

rem FUNCTIONS AT VERY BOTTOM
:TOUPPERCASE
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%%
)
goto :eof

Result

Make meeeee all uppercaseeeee! (Old variable)
MAKE MEEEEE ALL UPPERCASEEEEE!
Ste
  • 1,729
  • 1
  • 17
  • 27
0

This is probably the fastest way to convert string to upper case using batch file - it uses a macro ,it stores only the upper case letters in the loop using the way of how batch replaces strings and stores the produced string in a variable called result:

@echo off

set UpperCaseMacro=for /L %%n in (1 1 2) do if %%n==2 (for %%# 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 "result=!result:%%#=%%#!") else setlocal enableDelayedExpansion ^& set result=

set "string=SOme STrinG WiTH lowerCAse letterS and UPCase leTTErs"
%UpperCaseMacro%%string%

echo %result%
npocmaka
  • 55,367
  • 18
  • 148
  • 187