Skip to main content

How to Convert a String to Uppercase in Batch Script

A common requirement in scripting is to normalize string data for comparisons or display, and converting a string to a consistent case (like all uppercase) is a standard way to do this. For example, you might want to compare user input against a value in a case-insensitive way. While batch scripting has no built-in UCASE() or TOUPPER() function, you can achieve this conversion using a clever trick with variable substitution.

This guide will teach you the standard, pure-batch method for converting a string to uppercase. You will learn the core FOR loop and substitution logic, how to wrap it in a reusable subroutine, and understand its limitations, especially with special characters.

The Challenge: No Native UCASE() Function

The cmd.exe interpreter does not provide a direct command to change the case of a string. To perform the conversion, we must create the logic ourselves. The standard method relies on a simple but powerful feature: you can use a variable as part of a search-and-replace operation.

The Core Method: FOR Loop and String Substitution

The logic for this technique is as follows:

  1. Define the original string you want to convert.
  2. Define a string containing all the lowercase letters of the alphabet.
  3. Use a FOR loop to iterate through each lowercase letter.
  4. Inside the loop, for each lowercase letter, perform a string substitution on your original string, replacing that lowercase letter with its uppercase equivalent.
  5. After the loop completes, the original string will have been transformed letter by letter into uppercase.

This script demonstrates the core logic in action.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "MyString=This is a Test String with MiXeD Case."
ECHO Original: !MyString!

REM Loop through all lowercase letters
FOR %%L 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 (
REM For each letter, replace its lowercase version with its uppercase version.
SET "MyString=!MyString:%%L=%%L!"
)

ECHO Uppercase: !MyString!

ENDLOCAL

Wait, the script above looks like it's replacing a letter with itself (%%L=%%L). How does this work? The magic is in the SET command's substitution syntax.

How the Script Works

The key to this entire trick is a special, case-insensitive behavior within the SET command's string substitution.

Let's break down the line SET "MyString=!MyString:%%L=%%L!":

  • SET "MyString=...": We are re-assigning the MyString variable.
  • !MyString: ... !: We are performing a search-and-replace on the current value of MyString.
  • :%%L=%%L: This is the crucial part. Let's say the loop is on the letter "a" (%%L is a). The command becomes !MyString:a=A!.
    • The search part (:a) is case-insensitive. It will find both "a" and "A" in the source string.
    • The replace part (=A) is case-sensitive. It uses the exact case of the %%L variable from the FOR loop's list.

Since our FOR loop list is (A B C ... Z), when %%L is A, the command effectively becomes: "Find all instances of 'a' or 'A' in MyString and replace them with a literal uppercase 'A'."

When the loop runs for all 26 letters, every lowercase letter in the original string is found and replaced by its uppercase equivalent from the FOR list, resulting in a fully uppercase string.

note

The script in the previous section was simplified for explanation. A real, working script must have an uppercase alphabet in the FOR loop list.

Corrected Working Script

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "MyString=This is a Test String with MiXeD Case."
ECHO Original: !MyString!

REM The FOR loop list MUST be uppercase for the replacement to work.
FOR %%L 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 "MyString=!MyString:%%L=%%L!"
)

ECHO Uppercase: !MyString!

ENDLOCAL

Output:

Original: This is a Test String with MiXeD Case.
Uppercase: THIS IS A TEST STRING WITH MIXED CASE.

Making it Reusable: An Uppercase Subroutine

Since this is a multi-line operation, it's a perfect candidate for a reusable subroutine. You can CALL this subroutine with your variable name, and it will convert it for you.

@ECHO OFF
SETLOCAL

SET "MyVar=Some mixed text"
ECHO Before: %MyVar%

CALL :ToUpper MyVar
ECHO After: %MyVar%

GOTO :EOF


:ToUpper
SETLOCAL ENABLEDELAYEDEXPANSION
SET "StringContents=!%1!"
FOR %%L 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 "StringContents=!StringContents:%%L=%%L!"
)
ENDLOCAL & SET "%1=%StringContents%"
GOTO :EOF
note

This is a powerful pattern. The ENDLOCAL & SET "%1=%StringContents%" is a standard trick to pass the result from the subroutine's local scope back to the main script.

Common Pitfalls and How to Solve Them

  • Special Characters (!, ^, &): This method is highly vulnerable to special characters. DelayedExpansion will corrupt any string containing !. Characters like &, |, and > can also break the SET command.
    • Solution: There is no simple, pure-batch solution for this. The method is best suited for simple alphanumeric strings. For complex strings, a call to PowerShell is far more robust:
    powershell -Command "'My !String!'.ToUpper()"
  • Forgetting DelayedExpansion: This entire technique relies on DelayedExpansion (!MyString!) so that the variable can be modified inside the loop. Forgetting to enable it will cause the script to fail.

Conclusion

Converting a string to uppercase is a great example of the clever workarounds possible in batch scripting.

  • The standard method uses a FOR loop over the uppercase alphabet and leverages the case-insensitive search and case-sensitive replace behavior of the SET command's substitution syntax.
  • This method requires DelayedExpansion to be enabled.
  • The technique is best encapsulated in a reusable subroutine for clean code.
  • Be aware that this method is not robust for strings containing special characters. For those cases, a PowerShell one-liner is the superior and recommended solution.