Converting a "set" into lowercase

Solution 1:

How do I convert the contents of a variable to lower case?

Use the following batch file:

SETLOCAL ENABLEDELAYEDEXPANSION
set /p nickname="Please enter your uppercase name!:
rem save the name, use "s in case there are space in the name
set "_UpperNick=nickname"
call LoCase nickname
set "_LowerNick=nickname"
echo _UpperNick
echo _LowerNick
ENDLOCAL
GOTO:EOF

:LoCase
:: Subroutine to convert a variable VALUE to all lower case.
:: The argument for this subroutine is the variable NAME.
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!
GOTO:EOF

Source Batch files - How To ... Convert Strings To Upper Case Or Lower Case

Solution 2:

For a predictive character set, this substring Set !var:A=a! works, and only working with predefined substring in bat/cmd.

For this type of task, why not get a little help with c#, which can make it possible to work with unconventional accents and consonants è, È, ä, Ä, ñ, Ñ, ç, Ç etc.

Where the bat/cmd will generate c# code, it will be compiled and executed at run time ....

Which solves possible user inputs, in which the sequence comes with accents and the vowels/consonants are different from the conventional ones [a-z] and/or [A_Z]

Obs: - Also work with nicknames with spaces and/or with compose names


@echo off && setlocal EnableDelayedExpansion 

cd /d "%~dp0" && title <nul && title ...\%~dpnx0 /// !time:~0,8! !date!
set "_where=%__appdir__%where.exe" && set "_csc=%windir%\Microsoft.NET"
set "_findstr=%__APPDIR__%\findstr.exe" && del /q /f "%tmp%\ToUpLower.*" 2>nul >nul 

>"%temp%\ToUpLower.cs"^
   ( 
echo= using System; namespace SUQ1522019 ^{class Program ^{static void Main(string[] args^) ^{
echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-l"^) ^{Console.WriteLine(args[1].ToLower(^)^);^} 
echo= if (args.Length==2 ^&^& args[0].ToLower(^)=="-u"^) ^{Console.WriteLine(args[1].ToUpper(^)^);^}^}^}^}
   ) && set "_arg=/t:exe /out:"%temp%\ToUpLower.exe" "%temp%\ToUpLower.cs" /platform:anycpu "

:loop
if exist "%temp%\ToUpLower.exe" goto :next
for /l %%L in (4 -1 2)do for /f "tokens=*" %%i in ('!_where! /r "!_csc!" csc.exe ^|!_findstr! /lic:"\v%%~L\."')do (
   set "_compiller="%%~i" !_arg! /unsafe+ /w:0 /o /nologo" && call !_compiller! && set /p "_nick="Please enter nickname: "
   if not "!_nick!"=="" (goto :next) else cls && goto :loop
  )

:next
for /f tokens^=^*delims^=^"  %%# in ('echo[!_nick!') do for /f tokens^=^*delims^=^" %%U in ('
call "%temp%\ToUpLower.exe" -u "%%#"')do set "_up_case_nick=%%~U" && for /f tokens^=^*delims^=^" %%l in ('
call "%temp%\ToUpLower.exe" -l "%%#"')do set "_low_case_nick=%%~l" ) else goto :loop

echo/ Your input nickname is: !_nick!
echo/   Your input upcase is: !_up_case_nick!
echo/  Your input lowcase is: !_low_case_nick!

echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "." 
del /q /f "%tmp%\ToUpLower.*" >nul 2>nul && endlocal & goto :EOF

  • Outputs for input == To Up Lower
 Please enter nickname: To Up Lower

Your input nickname is: To Up Lower
  Your input upcase is: TO UP LOWER
 Your input lowcase is: to up lower

The ToUpLower.cs c# code with no escaping:


 using System; namespace SUQ1522019 {class Program {static void Main(string[] args) {
 if (args.Length==2 && args[0].ToLower()=="-l") {Console.WriteLine(args[1].ToLower());} 
 if (args.Length==2 && args[0].ToLower()=="-u") {Console.WriteLine(args[1].ToUpper());}}}}

The ToUpLower.cs c# code with no escaping and indented:


using System
namespace SUQ1522019 
{
   class Program 
   {
      static void Main(string[] args)
      {
         if (args.Length==2 && args[0].ToLower()=="-l") 
         {
            Console.WriteLine(args[1].ToLower());
         } 

         if (args.Length==2 && args[0].ToLower()=="-u") 
         {
            Console.WriteLine(args[1].ToUpper());
         }
      }
   }
}

  • This c# code was compiled/tested on csc.exe versions:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
c:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe

  • This is the command line used to compile the c# code:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:"%tmp%\ToUpLower.exe" "%tmp%\ToUpLower.cs" /platform:anycpu /unsafe+ /w:0 /o /nologo 

  • ToUpLower.exe usage UPPER to -> lower
ToUpLower.exe -l STRING 

:: if have space, use quote:

ToUpLower.exe -l "THIS STRING" 

:: or ..

ToUpLower.exe -L STRING

:: if have space, use quote:

ToUpLower.exe -L "THIS STRING"


  • ToUpLower.exe usage lower to -> UPPER

ToUpLower.exe -u string

:: if have space, use quote:

ToUpLower.exe -u "this string"

:: or ..

ToUpLower.exe -U string

:: if have space, use quote:

ToUpLower.exe -U "this string"


To keep ToUpLower.exe, remove echo/ from copy command:


echo/ >nul 2>nul copy "%tmp%\ToUpLower.exe" "." 
  • This command line will copy ToUpLower.exe from %temp% to same the same folder where your bat is running.

Programming Guide C#:

  • Args

  • ToLower

  • ToUpper


  • Edit: This code will compile the C# source code with the first .Net Framework found in this order:
c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
c:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe
c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
c:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe

  • A second option will be to add/join the ToUpLower.exe executable on the batch file, using base64 strings and decoding at run time .

    What you can do using %__APPDIR__%Certutil.exe, available on Windows.

Obs: - Also work with nicknames with spaces and/or with compose names


@echo off && setlocal EnableDelayedExpansion 

cd /d "%~dp0" && title <nul
title ...\%~dpnx0 /// !time:~0,8! !date!
del /q /f "%temp%\ToUpLower.exe" 2>nul >nul

%__APPDIR__%certutil.exe -f -decode "%~dpnx0" "%tmp%\ToUpLower.exe" 2>nul >nul

:loop
cls & echo/ & set /p "_nick="Please enter nickname: "

if not "!_nick!"==""  (
for /f tokens^=^*delims^=^"  %%# in ('echo[!_nick!') do for /f tokens^=^*delims^=^" %%U in ('
call "%temp%\ToUpLower.exe" -u "%%#"')do set "_up_case_nick=%%~U" && for /f tokens^=^*delims^=^" %%l in ('
call "%temp%\ToUpLower.exe" -l "%%#"')do set "_low_case_nick=%%~l" ) else goto :loop

echo/ Your input nickname is: !_nick!
echo/   Your input upcase is: !_up_case_nick!
echo/  Your input lowcase is: !_low_case_nick!

echo/ 2>nul >nul copy /y "%tmp%\ToUpLower.exe" "." 
del /q /f "%tmp%\ToUpLower.*" >nul 2>nul && endlocal & goto :EOF

rem :: Strings Base64 to decode in run time to generate/use the file: "%tmp%\ToUpLower.exe"

-----BEGIN CERTIFICATE----- TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUu
DQ0KJAAAAAAAAABQRQAATAEDAPztNl4AAAAAAAAAAOAAAgELAQgAAAYAAAAGAAAAAAAAHiUAAAAgAAAAQAAAAABAA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