Page 1 of 1

replacing text in file with an equal-sign in it

Posted: 04 Jul 2013 08:08
by shad0wman
Hi,

I think I´ve a simple problem, but can´t find out the solution.

I read a text-file line by line and want to replace some text.
That´s no problem so far, but the text to be replaced has an equal-sign (=) in it.

I don´t find out, how to mask this sign.

For better understanding, here my code:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set sourcefile="C:\filename_BACKUP.ini"
set targetfile="C:\filename.ini"
set pattern="=prod/in"
set replace="=/prod/in"

rename C:\filename.ini filename_BACKUP.ini
if exist %targetfile% (del /f %targetfile% 1>NUL 2>NUL)

for /F "delims=" %%a in ('findstr . "%sourcefile%"') do (
set text=%%a
set text=!text:%pattern%=%replace%!
echo !text!>>%targetfile%
)

many thanks for help!

Re: replacing text in file with an equal-sign in it

Posted: 04 Jul 2013 13:00
by jeb
Hi shadOwnman,

no it's not possible with your way, as there is no way to escape the equal sign.

But there exists some other solutions, which solve the search and replace with vbscript.

regex search and replace for batch - Easily edit files!

New regex utility to search and replace strings in files

Re: replacing text in file with an equal-sign in it

Posted: 04 Jul 2013 22:44
by dbenham
Either of the hybrid batch/JScript regex script links that jeb posted will work well. The hybrid scripts will far outperform any pure batch solution, and they have fewer limitations.

But before I rote my REPL.BAT hybrid script, I worte a pure batch script that performs decently. It can handle = no problem: The "ultimate" file search and replace batch utility

Dave Benham