I have made a batch script that replaces strings in a specified text-based file. Within my file, the string OutDir=bin should be replaced as OutDir=Build but gets output as bin=OutDir=Build=bin. How do I escape the = in OutDir=bin so that the string doesn't become garbled? I have tried OutDir^= but using the carrot doesn't work either.

Here is my current script:

@echo off 
    setlocal enableextensions disabledelayedexpansion

    set search=OutDir=bin
    set replace=OutDir=Build

    set textFile=%DOCUMENT%.txt

    for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%textFile%" echo(!line:%search%=%replace%!

I have attempted to change the find and replace variables to compensate for the = in the string I'm replacing but none have worked:

set search= "OutDir=bin"
set replace= "OutDir=Build"

set search="OutDir=bin"
set replace="OutDir=Build"

set "search=OutDir=bin"
set "replace=OutDir=Build"

"set search=OutDir=bin"
"set replace=OutDir=Build"
  • I assume you've already Googled this some but read over this and all the answers here (stackoverflow.com/questions/9556676/…) but the answer by dbenham in particular and the link he provides on DosTips for some additional ideas, etc. This may be easier to tackle with Batch using a little PowerShell if that's something you are willing to try—you should be able to put some PowerShell right into the batch and let it do it via PowerShell but it could be transparent if you write it correctly so you just have a batch script you run. – Pimp Juice IT Jan 4 '18 at 2:07
  • I am not familiar enough with PowerShell to even attempt it. This is being integrated with an existing batch script, so it must be batch. – Mr. Mendelli Jan 4 '18 at 2:10
  • 1
    I understand everything you are saying. In case you want to give it a simple try to see if it would potentially work and it gives you the desired output, look over the text here of a quick batch script you can run as batch just like you do any other batch script: justpaste.it/1f9xv. Please remember that PowerShell is Windows native and the language is really just text so if the exe conversion doesn't mess up text of a batch then it shouldn't do the same here. This should be simple enough for you to do a small test I suppose so in case you find it helpful, there you go. – Pimp Juice IT Jan 4 '18 at 2:56
  • 1
    Let me know how it goes and if you find anything helpful or needing adjusting or confirm it could work upon testing, etc. please let me know. I'll be happy to add an answer with much more detail if you think it could even be possible in your environment. – Pimp Juice IT Jan 4 '18 at 2:58
  • 1
    Thank you very much! I will test this as soon as I can. I'll get back to you with the results when my testing is complete. – Mr. Mendelli Jan 4 '18 at 3:03

Replacing the Equal Symbol = in a String with Batch using PowerShell

After testing and then research afterwards, I came upon this answer on StackOverflow which then also pointed to this thread and posts on DosTips and replacing the = with pure batch isn't easy.

When I run into issues with batch scripting where it takes a ton of complex batch logic or just cannot figure out how to get it to work easily, I usually try incorporating some PowerShell into the mix since it's Windows native and it can be much more robust than batch without complex logic.

The below batch script will essentially:

  • Use Get-Content and Replace for the string to search and replace
  • Then it will use Set-Content to put the newly replaced string back into the file accordingly


@echo off
setlocal enableextensions disabledelayedexpansion

set search=OutDir=bin
set replace=OutDir=Build
set textFile=%DOCUMENT%.txt

SET PSScript=%temp%\~tmpStrRplc.ps1
ECHO (Get-Content "%~dp0%textFile%").replace("%search%", "%replace%") ^| Set-Content "%~dp0%textFile%">"%PSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"

Output Result


Further Resources

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.