replacing text in file with an equal-sign in it

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
shad0wman
Posts: 1
Joined: 04 Jul 2013 07:58

replacing text in file with an equal-sign in it

#1 Post by shad0wman » 04 Jul 2013 08:08

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!

jeb
Expert
Posts: 1058
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

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

#2 Post by jeb » 04 Jul 2013 13:00

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#3 Post by dbenham » 04 Jul 2013 22:44

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

Post Reply