C:\_G\WWW\~ELISANET\INFO\tscmd039.html
<http://www.elisanet.fi/tsalmi/info/tscmd039.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 12-Oct-2018 02:41:51

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



39} Is there a subroutine to convert a variable into uppercase?

As below. Note that the substitution method is case insensitive, which means that while working for this application, it is not useful for all character substitution tasks. Also note the minor detail of the Scandinavian character conversion.
  @echo off & setlocal enableextensions
  set myvar_=My Test String!  åöä
  echo %myvar_%
  call :ToUpcase "%myvar_%" myvar_
  echo %myvar_%
  endlocal & goto :EOF
  ::
  :: ======================

  :ToUpcase
  setlocal enableextensions
    set var_=%1
    set var_=%var_:a=A%
    set var_=%var_:b=B%
    set var_=%var_:c=C%
    set var_=%var_:d=D%
    set var_=%var_:e=E%
    set var_=%var_:f=F%
    set var_=%var_:g=G%
    set var_=%var_:h=H%
    set var_=%var_:i=I%
    set var_=%var_:j=J%
    set var_=%var_:k=K%
    set var_=%var_:l=L%
    set var_=%var_:m=M%
    set var_=%var_:n=N%
    set var_=%var_:o=O%
    set var_=%var_:p=P%
    set var_=%var_:q=Q%
    set var_=%var_:r=R%
    set var_=%var_:s=S%
    set var_=%var_:t=T%
    set var_=%var_:u=U%
    set var_=%var_:v=V%
    set var_=%var_:w=W%
    set var_=%var_:x=X%
    set var_=%var_:y=Y%
    set var_=%var_:z=Z%
    set var_=%var_:å=Å%
    set var_=%var_:ä=Ä%
    set var_=%var_:ö=Ö%
    set var_=%var_:"=%
  endlocal & set %2=%var_%& goto :EOF

The output is
  C:\_D\TEST>cmdfaq
  My Test String!  åöä
  MY TEST STRING!  ÅÖÄ

More concisely, one can capitalize (if you pardon the pun) on the fact that in for and the substitution lower and upper case source are equivalent.
  @echo off & setlocal enableextensions
  set myvar_=My Test String!  åöä
  echo %myvar_%
  call :ToUpcaseWithFor "%myvar_%" myvar_
  echo %myvar_%
  endlocal & goto :EOF
  ::
  :: ======================

  :ToUpcaseWithFor
  setlocal enableextensions enabledelayedexpansion
  set var_=%~1
  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 (
    set var_=!var_:%%c=%%c!
    )
  endlocal & set %2=%var_%& goto :EOF

The ouput is
  C:\_D\TEST>cmdfaq
  My Test String!  åöä
  MY TEST STRING  ÅÖÄ

Another option is
  @echo off & setlocal enableextensions
  set myvar_=My Test String!  åöä
  echo %myvar_%
  call :ToUpcaseWithGawk "%myvar_%" myvar_
  echo %myvar_%
  endlocal & goto :EOF
  ::
  :: ================================================================

  :ToUpcaseWithGawk
  :: Requires G(nu)AWK
  setlocal enableextensions
    gawk 'BEGIN{printf"@set var_=%%s\n",toupper(%1)}'>"%temp%\tmp$$$.cmd"
    for %%c in (call del) do %%c "%temp%\tmp$$$.cmd"
  endlocal & set %2=%var_% & goto :EOF

The output is
  C:\_D\TEST>cmdfaq
  My Test String!  åöä
  MY TEST STRING!  åöä

  @echo off & setlocal enableextensions
  rem The same with GnuWin32 gawk (let's call it unxgawk)
  set myvar_=My Test String!  åöä
  echo %myvar_%
  call :ToUpcaseWithUnxGawk "%myvar_%" myvar_
  echo %myvar_%
  endlocal & goto :EOF
  ::
  :: ================================================================

  :ToUpcaseWithUnxGawk
  setlocal enableextensions
  rem enabledelayedexpansion
    unxgawk "BEGIN{printf\"@set var_=%%s\n\",toupper(\"%~1\")}">"%temp%\tmp$$$.cmd"
    for %%c in (call del) do %%c "%temp%\tmp$$$.cmd"
  endlocal & set %2=%var_% & goto :EOF

The output is
  C:\_D\TEST>cmdfaq
  My Test String!  åöä
  MY TEST STRING!  Õ÷õ

Another script-only method. More complicated, but on the side demonstrates several features of cmd script programming.
  @echo off & setlocal enableextensions enabledelayedexpansion
  ::
  :: Constants for the lower and upper case

  set dn=abcdefghijklmnopqrstuvwxyzåäö
  set up=ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ
  ::
  :: Set a test string

  if "%~1"=="" (
    set Str=This is a test string
    ) else (
    set Str=%*
    )
  ::
  :: Get the length of the string assuming a max of 255

  set charCount=0
  for /l %%i in (0,1,254) do (
    set char=!Str:~%%i,1!
    if defined char set /a charCount+=1)
  ::
  :: Upcase character by character

  set StrUp=
  for /l %%i in (0,1,%charCount%) do (
    set char=!Str:~%%i,1!
    for /l %%p in (0,1,28) do (
      if [!char!]==[!dn:~%%p^,1!] set char=!up:~%%p,1!
      )
    set StrUp=!StrUp!!char!
    )
  ::
  :: Display the result

  echo %Str%
  echo %StrUp%
  endlocal & goto :EOF

An example of the output
  C:\_D\TEST>cmdfaq  Hello  World!  åöä
  Hello  World  åöä
  HELLO  WORLD  ÅÖÄ

There are other options. Using SED we can have
  @echo off & setlocal enableextensions
  if defined ProgramW6432 (
    echo/
    echo Exiting: %~f0 is incompatible with 64-bit Windows (but read on)
    goto :EOF)
  ::
  set myvar_=My Test String!  åöä
  echo %myvar_%
  call :ToUpcaseWithSed "%myvar_%" myvar_
  echo %myvar_%
  endlocal & goto :EOF
  ::
  :: ====================================================================

  :ToUpcaseWithSed
  :: Requires SED
  setlocal enableextensions
  for /f "tokens=*" %%f in ("%temp%") do set temp_=%%~sf
  echo @set var_=%1^
    |sed "y/abcdefghijklmnopqrstuvwxyzåöä/ABCDEFGHIJKLMNOPQRSTUVWXYZÅÖÄ/"^
    |sed -e "s/\"//g">%temp_%\tmp$$$.cmd
  for %%c in (call del) do %%c %temp_%\tmp$$$.cmd
  endlocal & set %2=%var_% & goto :EOF

The output is
  C:\_D\TEST>cmdfaq
  My Test String!  åöä
  MY TEST STRING!  ÅÖÄ

  @echo off & setlocal enableextensions
  :: The same with GnuWin32 sed and tr (let's call them unxsed and unxtr)
  set myvar_=My Test String!  åöä
  echo %myvar_%
  call :ToUpcaseWithUnxSedTr "%myvar_%" myvar_
  echo %myvar_%
  endlocal & goto :EOF
  ::
  :: ====================================================================

  :ToUpcaseWithUnxSedTr
  setlocal enableextensions
  echo @set var_=%~1^
    |unxsed y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/^
    |unxtr \206\204\224 \217\216\231^
    > %temp%\tmp$$$.cmd
    rem      å   ä   ö    Å   Ä   Ö   in octal
  for %%c in (call del) do %%c %temp%\tmp$$$.cmd
  endlocal & set %2=%var_% & goto :EOF

The output is
  C:\_D\TEST>cmdfaq
  My Test String!  åöä
  MY TEST STRING!  ÅÖÄ

The conversion can also be done with a Visual Basic Script (VBScript) aided command line script
  @echo off & setlocal enableextensions
  ::
  :: The text to be concerved to upper case

  set MyText_=Scripts are fun, or are they?
  ::
  :: Build a Visual Basic Script

  set skip=
  findstr "'%skip%VBS" "%~f0" > "%temp%\tmp$$$.vbs"
  :: Run it with Microsoft Windows Script Host Version 5.6
  cscript //nologo "%temp%\tmp$$$.vbs"
  :: Call the command line script wchich the script host built
  call "tmp$$$.cmd"
  :: Clean up
  for %%f in ("%temp%\tmp$$$.vbs"
              "tmp$$$.cmd") do if exist %%f del %%f
  ::
  :: Demonstrate the result

  echo %MyText_%
  echo %MyTextUp_%
  endlocal & goto :EOF
  '
  'The Visual Basic Script

  Dim MyString, MyStringUp 'VBS
  Set WshShell = WScript.CreateObject("WScript.Shell") 'VBS
  MyString=WshShell.ExpandEnvironmentStrings("%MyText_%") 'VBS
  MyStringUp = UCase(MyString) 'VBS
  WshShell.Run "cmd /c echo @set MyTextUp_=" & MyStringUp & ">tmp$$$.cmd" 'VBS

The output
  C:\_D\TEST>cmdfaq
  Scripts are fun, or are they?
  SCRIPTS ARE FUN, OR ARE THEY?

or, more concisely
  @echo off & setlocal enableextensions
  ::
  :: The text to be concerved to upper case

  set MyText_=Scripts are fun, or are they?
  ::
  :: Utilize a Visual Basic Script

  echo Wscript.Echo UCase^("%MyText_%"^)>"%temp%\tmp$$$.vbs"
  type "%temp%\tmp$$$.vbs"
  for /f "tokens=* delims=" %%s in (
    'cscript //nologo "%temp%\tmp$$$.vbs"') do set MyTextUp_=%%s
  ::
  :: Demonstrate the result

  echo %MyText_%
  echo %MyTextUp_%
  endlocal & goto :EOF

References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Dec 20 2003, 11:54 am [M]
  Google Groups Dec 20 2003, 11:15 pm [M]
  Google Groups 21 Feb 2011 14:05:29 +0800
  Also see tsbat.zip item #7
  Number converter - hex, octal, binary | Coder’s Toolbox

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd039.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd039
http://www.elisanet.fi/tsalmi/info/tscmd039.html
file:///c:/_g/www/~elisanet/info/tscmd039.html