Batch file: How to replace "=" (equal signs) and a string variable?

batch string replace special characters
batch file replace line in text file
batch script to find and replace text in a file
batch file replace string in filename
batch file remove characters from string
text substitution in batch files
batch regex replace
batch file right string

Besides SED, how can an equal sign be replaced? And how can I use a string variable in string replacement?

Consider this example:

For /F "tokens=*" %%B IN (test.txt) DO (
   SET t=is
   SET old=%%B
   SET new=!old:t=!
   ECHO !new!


:: SET new=!old:==!

Two problems:

First, I cannot use the variable %t% in !:=!.

   SET t=is
   SET old=%%B
   SET new=!old:t=!

Second, I cannot replace the equal sign in the command line

   SET new=!old:==!

I just created a simple solution for this myself, maybe it helps someone.

The disadvantage (or advantage, depends on what you want to do) is that multiple equal signs one after another get handled like one single equal sign. (example: "str==ing" gives the same output as "str=ing")

@echo off
set "x=this is=an test="
echo x=%x%

call :replaceEqualSign in x with _
echo x=%x%


:replaceEqualSign in <variable> with <newString>
    setlocal enableDelayedExpansion

        set "_s=!%~2!#"
        set "_r="

            for /F "tokens=1* delims==" %%A in ("%_s%") do (
                if not defined _r ( set "_r=%%A" ) else ( set "_r=%_r%%~4%%A" )
                set "_s=%%B"
        if defined _s goto _replaceEqualSign

    endlocal&set "%~2=%_r:~0,-1%"
exit /B

As you have seen, you use the function like this:

call :replaceEqualSign in variableName with newString

Batch Script - Replace a String, Batch Script Tutorial; Batch Script - Home · Batch Script - Overview · Batch Script - Environment · Batch Script - Commands · Batch Script - Files · Batch Script -  Creating simple batch file. Open Start . Search for Notepad , and click the top result to start the app. Type the following lines to create a batch file: @ECHO OFF ECHO Congratulations! Your first batch file executed successfully. PAUSE. Source @ECHO OFF — Disables the display prompt to show

The setlocal enableDelayedExpansion should be moved after your old=%%B assignment in case %%B contains !.

The "t" problem is easy to solve within a loop by using another FOR variable

For /F "tokens=*" %%B IN (test.txt) DO (
   SET t=is
   SET old=%%B
   for /f %%T in ("!t!") do SET new=!old:%%T=!
   ECHO !new!

There is no simple native batch solution for replacing =. You can iterate through the string, character by character, but that is slow. Your best bet is probably to switch to VBScript or JScript, or use a non-native utility.

If you really want to do this using pure Windows batch commands, there are a couple of interesting ideas at

CMD Variable edit replace - Windows CMD, Use the syntax below to edit and replace the characters assigned to a string variable. If the batch parameter was supplied as %2 then this would be: ECHO​  Place repl.bat in the same folder as the batch file or in a folder that is on the path. Repl.bat is a hybrid batch file using native Windows scripting and is far faster than a regular batch script. The L switch makes the text search and replace a literal string and I'd expect the 12 MB file to complete in several seconds on a modern PC.

Why not use Edlin? I could not find a way to do this with one initial file and no errors from Edlin, but just ignore them with NUL:. Strangly, the TYPE %0 includes the whole file even if there's an end of file character between the = and !, using TYPE on the batch file after it has run will not work the same way.

GOTO skip

SET "new==old============="

ECHO %new% > %TEMP%\var.tmp
TYPE %0 > %TEMP%\edlin.tmp

EDLIN %TEMP%\var.tmp < %TEMP%\edlin.tmp > NUL:

SET /P newnew=<%TEMP%\VAR.TMP
ECHO %newnew%

ERASE %TEMP%\edlin.tmp

How to replace character "-" by "%_%" in a batch file?, Because of the way percents are used for variables it messes up what you are trying to do. One way around this is with delayed expansion. Batch, String, Substitution, Replace, Search In a batch file there is a syntax that can be used to replace one value with another in variables. This can be used in lots of ways and I show some of them here.

String substitution and replacing in batch files, In a batch file there is a syntax that can be used to replace one value with another in variables. This can be used in lots of ways and I show  I have written a batch script to find and replace a string in a text file. Following is my script. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set "newfile=Ou

My answer from another post, but it applies here, too:

There is an alternative that is easier. Instead of passing in a value that contains an equals sign, try something like a colon instead. Then, through the ability to modify that value (the colon), you can convert it back into an equals. Here is an example:

@echo off
set VALUE1=%1
set VALUE2=%VALUE1::==%
echo value1 = %VALUE1%
echo value2 = %VALUE2%

When you run the batch file, call it like this:

C:\>myBatch name:someValue

The output would be:

value1 = name:someValue
value2 = name=someValue

If the name or value contains a space, you will have other issues to address, though. You will need to wrap the entire string in double quotes. But, then you have the issue of needing to get rid of them. This can also be handled, like this:

@echo off
set PARAM=%1
set BASE=%PARAM:"=%
set PAIR=%BASE::==%

rem Either of these two lines will do the same thing - just notice the 'delims'
rem for /f "tokens=1,2 delims=:" %%a in ("%BASE%") do set NAME=%%a & set VALUE=%%b
rem for /f "tokens=1,2 delims==" %%a in ("%PAIR%") do set NAME=%%a & set VALUE=%%b

for /f "tokens=1,2 delims=:" %%a in ("%BASE%") do set NAME=%%a & set VALUE=%%b

echo param = %PARAM%
echo base  = %BASE%
echo pair  = %PAIR%
echo name  = %NAME%
echo value = %VALUE%

When running this batch file like this:

C:\>myBatch "some name:another value"

The output will be:

param = "some name:another value"
base  = some name:another value
pair  = some name=another value
name  = some name
value = another value

Hope that helps others in their quest to win the fight with batch files.

Mike V.

Replace text in a file using Batch script - Victor Leung, Yesterday I was working at a client site. It is a Windows server isolated from external internet access and prohibited to install any new software. I got a task in​  Saving the Batch File Finish entering your batch file's text. Click File. Click Save As…. Enter a name and the ".bat" extension. Click the "Save as type" drop-down box. Click All Files. Select a save location. Click Save. Close your Notepad file. Edit the batch file's contents.

How to search & replace a string in text file using a batch file, I want to write a batch file which can look through a textfile and find a string and replace it with another string. This two string will not be  Type a new name for the file and then hit Enter or click somewhere else in the window. All the selected files are renamed using the name you just typed, and are appended with a number in parentheses to differentiate them. Rename Multiple Files from the Command Prompt. If you need more power than that, you can use the rename or ren command in a

DOS Batch - Find and Replace, Search a file and replace all occurrences of a string with another string. Description: This batch allows string substitution in a text file. It parses each line of a text file  Quick tip: You can also press the F2 button, right-click the files and select the Rename option, or click the file once and slowly click the file name again to trigger the rename action. Confirm

How to replace multiple tags in XML document in batch file , It is probably simpler to use Powershell for such things, see example here: How can you find and replace text in a file using the Windows  Two batch files that supply search and replace functions have been written by Stack Overflow members dbenham and aacini using native built-in jscript in Windows. They are both robust and very swift with large files compared to plain batch scripting, and also simpler to use for basic replacing of text.

  • Are you really working in DOS, or do you just mean the windows command prompt?
  • Well, of course %t% cannot work as it gets replaced before the loop even runs.
  • Damien, this is obviously cmd.
  • @Joey - of course it is - but I sometimes adopt a rhetorical style to try to make people realize that they're being inexact in their terminology.
  • Damien, I have long given up on that, I just silently edit ;)
  • I see. Maybe I need to approach VBScript, VB or C#. It seems they're an advanced version of DOS Command. How about if the text is like this: This is an apple. This is a cat. Using the codes gets rid of all the "is". We do know how to do whole word only replace in VB but how about in DOS Command? Is it impossible? The same to the equal sign case?
  • If you only want to replace whole words, then batch script is not a good option. It can be approximated, but you would be much better off using something that supports regex search and replace, such as VBScript.
  • Yes. I see. Thanks for pointing me to the suitable languages.
  • "Maybe I need to approach VBScript, VB or C#. It seems they're an advanced version of DOS Command." +1 for "C# is an advanced version of DOS Command"...
  • The end of file character got deleted when I posted. It should be between the = and ! on the third line.