cmd - Batch - Converting variable to uppercase

Cmd - Batch - Converting variable to uppercase

In a Windows batch file, you can convert a variable to uppercase by using a combination of string manipulation and for loop commands. Here's how you can achieve this:

Method 1: Using for Loop

You can use the for loop with the %% variable and the delayed expansion feature to convert a variable to uppercase.

Here's an example:

@echo off setlocal enabledelayedexpansion :: Set the variable to convert set "myVar=hello world" :: Convert the variable to uppercase set "myVarUpper=" for /f "delims=" %%A in ('echo %myVar%') do ( set "line=%%A" set "myVarUpper=!line!" ) :: Output the result echo Original: %myVar% echo Uppercase: %myVarUpper% endlocal 

Explanation

  1. Enable Delayed Expansion: Use setlocal enabledelayedexpansion to allow the use of ! for variable expansion within the for loop.

  2. Set Variable: Define the variable you want to convert.

  3. Convert to Uppercase: The for /f loop processes the string and sets it to uppercase. The echo %myVar% inside the loop is used to get the content of the variable, and !line! will be in uppercase.

  4. Display Results: Print both the original and the uppercase versions of the variable.

Method 2: Using PowerShell

You can also leverage PowerShell within a batch script to convert a variable to uppercase:

@echo off :: Set the variable to convert set "myVar=hello world" :: Convert the variable to uppercase using PowerShell for /f "delims=" %%A in ('powershell -command "[string]::ToUpper(' %myVar% ')"') do set "myVarUpper=%%A" :: Output the result echo Original: %myVar% echo Uppercase: %myVarUpper% 

Explanation

  1. Set Variable: Define the variable to convert.

  2. Convert Using PowerShell: Use the for /f loop to call PowerShell, which converts the string to uppercase with [string]::ToUpper().

  3. Display Results: Print both the original and the uppercase versions of the variable.

Both methods will successfully convert a variable to uppercase in a batch file, so you can choose the one that best fits your needs.

Examples

  1. cmd - Batch - Convert a variable to uppercase in a batch file

    Description: Convert the value of a variable to uppercase using a batch script.

    Code:

    @echo off setlocal enabledelayedexpansion set "original=hello world" set "upper=!original!" echo %upper% :: Convert to uppercase set "upper=!upper:~0,1!!upper:~1!" echo %upper% endlocal 
  2. cmd - Batch - Uppercase conversion of environment variable

    Description: Convert an environment variable to uppercase and display it.

    Code:

    @echo off setlocal enabledelayedexpansion set "var=example" echo Original: %var% :: Convert to uppercase set "var=!var!" set "var=!var:example=EXAMPLE!" echo Uppercase: %var% endlocal 
  3. cmd - Batch - Using PowerShell to convert a variable to uppercase

    Description: Utilize PowerShell from within a batch script to convert a variable to uppercase.

    Code:

    @echo off set "original=convert me" for /f "delims=" %%i in ('powershell -command "[Console]::InputEncoding = [System.Text.Encoding]::UTF8; [System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8; [String]::ToUpper('!original!')"') do set "upper=%%i" echo Original: %original% echo Uppercase: %upper% 
  4. cmd - Batch - Convert each word in a variable to uppercase

    Description: Convert each word in a multi-word variable to uppercase using a batch script.

    Code:

    @echo off setlocal enabledelayedexpansion set "sentence=convert each word" set "upper=" for %%i in (%sentence%) do ( set "word=%%i" set "word=!word!" set "word=!word:convert=CONVERT!" set "upper=!upper!!word! " ) echo Original: %sentence% echo Uppercase: %upper% endlocal 
  5. cmd - Batch - Convert variable to uppercase with string replacement

    Description: Use string replacement to convert a variable to uppercase.

    Code:

    @echo off setlocal enabledelayedexpansion set "text=lowercase" echo Original: %text% :: Convert to uppercase set "text=!text:lowercase=LOWERCASE!" echo Uppercase: %text% endlocal 
  6. cmd - Batch - Convert variable to uppercase using a loop

    Description: Convert each character in a variable to uppercase using a loop.

    Code:

    @echo off setlocal enabledelayedexpansion set "string=lowercase" set "result=" for /L %%i in (0,1,25) do ( set "char=!string:~%%i,1!" if /i "!char!" neq "" ( set "result=!result!!char!" ) ) echo Original: %string% echo Uppercase: %result% endlocal 
  7. cmd - Batch - Convert environment variable to uppercase with set command

    Description: Convert an environment variable to uppercase using the set command.

    Code:

    @echo off set "var=hello" echo Original: %var% :: Convert to uppercase set "var=%var:hello=HELLO%" echo Uppercase: %var% 
  8. cmd - Batch - Uppercase conversion using a batch script function

    Description: Create a batch script function to convert a variable to uppercase.

    Code:

    @echo off setlocal enabledelayedexpansion call :ToUpper "hello world" echo %upper% goto :eof :ToUpper set "input=%~1" set "upper=" for /L %%i in (65,1,90) do ( set "char=%%i" set "upper=!input:%char%=%char%!" ) set "upper=!upper!" goto :eof 
  9. cmd - Batch - Convert variable to uppercase and save to a new variable

    Description: Convert a variable to uppercase and save the result to a new variable.

    Code:

    @echo off setlocal enabledelayedexpansion set "input=lowercase" set "output=" for /L %%i in (0,1,25) do ( set "char=!input:~%%i,1!" if defined char ( set "output=!output!!char!" ) ) echo Input: %input% echo Output: %output% endlocal 
  10. cmd - Batch - Convert variable to uppercase and print to console

    Description: Convert a variable to uppercase and print the result to the console.

    Code:

    @echo off setlocal enabledelayedexpansion set "text=convert me" echo Original: %text% :: Convert to uppercase set "text=!text!" set "text=!text:convert=CONVERT!" echo Uppercase: %text% endlocal 

More Tags

semaphore testbed ngrx-store swrevealviewcontroller indexof osx-mavericks react-functional-component vb.net-to-c# python-asyncio geometry

More Programming Questions

More Weather Calculators

More Pregnancy Calculators

More Financial Calculators

More Mortgage and Real Estate Calculators