How to Remove a Character from a String in Batch Script
A common string manipulation task is removing specific, unwanted characters. You might need to sanitize a filename by removing illegal characters, strip commas from a number, or remove punctuation from a data string before processing it. While batch scripting has no dedicated REMOVE() function, it has a simple and powerful built-in method for this using string substitution.
This guide will teach you how to use the SET command's substitution syntax to remove the first, or all, occurrences of a specific character or substring. You will learn how this feature works, its case-sensitivity, and how to use it safely within a loop.
The Challenge: No Native REMOVE() Function
The cmd.exe interpreter provides a syntax for replacing a substring with another, but not for simply removing one. The trick is to realize that "removing" a character is the same as "replacing it with nothing."
The Core Method: SET Substitution with an Empty String
The entire mechanism for this task is built into the SET command's variable expansion.
The standard replacement syntax is: %VariableName:StringToFind=StringToReplace%
To remove StringToFind, we simply make StringToReplace an empty string.
The syntax for removal is: %VariableName:StringToFind=%
Basic Example: Removing a Single Character
Let's start with a simple string and remove a hyphen from it.
@ECHO OFF
SET "MyString=Hello-World"
ECHO Original String: %MyString%
REM Replace the hyphen with nothing.
ECHO Modified String: %MyString:-=%
Output:
Original String: Hello-World
Modified String: HelloWorld
Removing All Occurrences of a Character
A key feature of this syntax is that it replaces all occurrences of the search string by default, not just the first one. This makes it perfect for tasks like stripping all spaces or all commas from a string.
@ECHO OFF
SET "MyNumber=1,000,000"
ECHO Original Number String: %MyNumber%
REM Remove all commas.
ECHO Number without Commas: %MyNumber:,=%
Output:
Original Number String: 1,000,000
Number without Commas: 1000000
Storing the Result in a New Variable
In a script, you'll almost always want to store the modified string in a new variable for later use.
@ECHO OFF
SET "OriginalPath=C:\Users\Admin\Documents\"
ECHO Original Path: %OriginalPath%
REM Create a new variable with the backslashes removed.
SET "CleanPath=%OriginalPath:\=%"
ECHO Cleaned Path: %CleanPath%
Output:
Original Path: C:\Users\Admin\Documents\
Cleaned Path: C:UsersAdminDocuments
Common Pitfalls and How to Solve Them
Problem: Case-Sensitivity
The string substitution is case-sensitive. If you try to remove a lowercase "a", it will not affect an uppercase "A".
An example of script with error:
@ECHO OFF
SET "MyString=Apple and apple"
ECHO %MyString:a=%
Output where only the lowercase 'a' is removed:
Apple nd pple
Solution: If you need a case-insensitive removal, you must run the replacement command for both the lowercase and uppercase versions of the character.
SET "NewString=%MyString:a=%" & SET "NewString=%NewString:A=%"
Problem: Removing Special Characters (&, |, >)
If the character you are trying to remove is a special command interpreter character, it can break the SET command.
@ECHO OFF
SET "MyString=Salt&Pepper"
REM This will FAIL because & is a command separator.
SET "NewString=%MyString:&=%"
Solution: Quote the Entire Assignment
The most robust way to write a SET command is to enclose the entire assignment expression in double quotes. This tells cmd.exe to treat everything after the first = as a literal string.
REM This is the correct, safe syntax.
SET "NewString=%MyString:&=%"
Problem: Removing Characters Inside a Loop
If you are modifying a variable inside a FOR loop, you must use delayed expansion. Standard %Var% expansion happens only once before the loop begins.
Solution: Use Delayed Expansion
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "MyString=A-B-C-"
FOR /L %%i IN (1,1,3) DO (
REM Remove the first hyphen from the CURRENT string.
SET "MyString=!MyString:-=!"
ECHO Iteration %%i: !MyString!
)
Output:
Iteration 1: AB-C-
Iteration 2: ABC-
Iteration 3: ABC
This specific example only removes one hyphen per iteration because of the * prefix (!MyString:*-=!), which is a different syntax for "find and replace first." The standard !MyString:-=! would remove all hyphens on the first iteration.
Practical Example: Sanitizing a Filename
This script takes a "dirty" string and removes a set of characters that are illegal in Windows filenames, producing a clean, safe filename.
@ECHO OFF
SETLOCAL
SET "DirtyName=This is a > report for Q1 < ready!.txt"
ECHO Original name: "%DirtyName%"
REM Chain multiple replacement operations.
SET "CleanName=%DirtyName:>=%"
SET "CleanName=%CleanName:<=%"
SET "CleanName:!=%"
SET "CleanName=%CleanName:"=%"
ECHO.
ECHO Sanitized name: "%CleanName%"
REM REN "SomeFile.tmp" "%CleanName%"
ENDLOCAL
Output:
Original name: "This is a > report for Q1 < ready!.txt"
Sanitized name: "This is a report for Q1 ready.txt"
Conclusion
The SET command's string substitution is a simple, fast, and powerful tool for removing characters from a string in a batch script.
Key takeaways:
- The core syntax is
%VAR:find=%, which replacesfindwith nothing. - The operation is case-sensitive and removes all occurrences by default.
- For robust scripting, quote your
SETassignments (SET "NewVar=...") to handle special characters. - When modifying a string inside a
FORloop, you must use delayed expansion (!VAR:find=%).