Replace a string from a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

Replace a string from a file

#1 Post by Mahendra » 06 Dec 2012 11:13

Hi

Good Morning all....could anybody please help me to achieve solution for this. ...Thanks in advance.

i have a configuration file qm.ini like below.....i want to replace a string , if config file contains "LogBufferPages=0"

LogBufferPages=0 will be replaced "LogBufferPages=512" with out modifying any other alignment/text in the config file.....




qm.ini
Log:
LogPrimaryFiles=3
LogSecondaryFiles=2
LogFilePages=1024
LogType=CIRCULAR
LogBufferPages=0
LogPath=/var/mqm/log/saturn!queue!manager/
Channels:
MaxChannels=20
MaxActiveChannels=100
MQIBindType=STANDARD

TCP:
KeepAlive = Yes

QMErrorLog:
ErrorLogSize=262144
ExcludeMessage=7234
SuppressMessage=9001,9002,9202
SuppressInterval=30

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Replace a string from a file

#2 Post by foxidrive » 06 Dec 2012 11:34

Run this in the folder with qm.ini inside it.

Note that lines like this will also be converted:
LogBufferPages=0100 to LogBufferPages=512100


Code: Select all

@set @JScript=1/*
@echo off
setlocal

cscript //nologo //E:JScript "%~f0"
move fileout.txt qm.ini
goto :eof

*/

// JScript
//
var ForReading= 1
var ForWriting = 2

var fso = new ActiveXObject("Scripting.FileSystemObject");

// paths must have doubled backslashes if used
// var input = fso.OpenTextFile("c:\\temp\\filein.txt", ForReading)
// var output = fso.OpenTextFile("c:\\temp\\fileout.txt", ForWriting, true)

var input = fso.OpenTextFile("qm.ini", ForReading)
var output = fso.OpenTextFile("fileout.txt", ForWriting, true)


var data = input.Readall()
data = data.replace("LogBufferPages=0","LogBufferPages=512")

output.Write(data)

input.Close()
output.Close()

Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

Re: Replace a string from a file

#3 Post by Mahendra » 06 Dec 2012 11:48

i am sorry ...only batch script permited


i have a configuration file qm.ini like below.....i want to replace a string , if config file contains "LogBufferPages=0"

LogBufferPages=0 will be replaced "LogBufferPages=512" with out modifying any other alignment/text in the config file.....




qm.ini
Log:
LogPrimaryFiles=3
LogSecondaryFiles=2
LogFilePages=1024
LogType=CIRCULAR
LogBufferPages=0
Channels:
MaxChannels=20
MaxActiveChannels=100
MQIBindType=STANDARD

TCP:
KeepAlive = Yes

QMErrorLog:
ErrorLogSize=262144
ExcludeMessage=7234
SuppressMessage=9001,9002,9202
SuppressInterval=30

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Replace a string from a file

#4 Post by foxidrive » 06 Dec 2012 12:04

Mahendra wrote:i am sorry ...only batch script permited


It is a batch script. It uses Windows Scripting Host which is on every computer from Win2K and later IIRC

Mahendra
Posts: 26
Joined: 23 Sep 2012 02:29

Re: Replace a string from a file

#5 Post by Mahendra » 06 Dec 2012 12:07

yes, i know......some part of the job suppose to be done with Batch script only ...so it will be required to be done everything with batch job only.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Replace a string from a file

#6 Post by foxidrive » 06 Dec 2012 12:23

Batch files use internal commands and many external commands: here are four external commands.
more.exe
findstr.exe
find.exe
sort.exe

along with more external commands. Windows scripting host is just another external command that a batch file can use. It is built into Windows.

Did you try the batch file I gave you?

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Replace a string from a file

#7 Post by Aacini » 06 Dec 2012 13:03

The Batch file below do what you want. It also preserve empty lines:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
for /F "delims=:" %%a in ('findstr /N "LogBufferPages=0" qm.ini') do set line=%%a
if defined line (
   set /A line-=1
   copy qm.ini qm.tmp > NUL
   echo :EOF>> qm.tmp
   call :ProcessFile !line! < qm.tmp > qm.ini
) else (
   echo Search string not found
)
goto :EOF

:ProcessFile targetLine
rem Read first line
set /P line=
rem Copy lines before target line
for /L %%i in (1,1,%1) do set /P line=!line!& echo/
rem Replace the target line
echo LogBufferPages=512
rem Copy lines after target line
:nextLine
   set line=
   set /P line=
   if "!line:~0,4!" equ ":EOF" exit /B
   echo(!line!
goto nextLine


Antonio

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Replace a string from a file

#8 Post by abc0502 » 06 Dec 2012 15:17

Hi, Antonio
Nice work, i was trying to do that but was stuck,
When i test the batch, i replaced the words to replace with a variables and also the input file so it work when you provide the file, word to replace and word to replace with.
But, it seems there is a small problem, always the first empty line is filled with the previous line before it

IF file like this
line1

line2
and we will replace line2 with the word test it will look like this
line1
line1
test

I tried to fix that but just can't follow the code

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Replace a string from a file

#9 Post by Aacini » 07 Dec 2012 17:58

abc0502 wrote:But, it seems there is a small problem, always the first empty line is filled with the previous line before it

Oops, you are right! My code works only if there are not empty lines before the target line (like in the original request). To fix this bug, modify ProcessFile subroutine this way:

Code: Select all

:ProcessFile targetLine
rem Copy lines before target line
for /L %%i in (1,1,%1) do (
   set line=
   set /P line=
   echo(!line!
)
rem Replace the target line
set /P line=
echo LogBufferPages=512
rem Copy lines after target line
:nextLine
   set line=
   set /P line=
   if "!line:~0,4!" equ ":EOF" exit /B
   echo(!line!
goto nextLine

Antonio

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Replace a string from a file

#10 Post by abc0502 » 08 Dec 2012 00:47

Thanks a lot Antonio :D
It works now fine, this is how i made it to take input file and word to replace and word to replace with if any one wants to make it as a function.

Code: Select all

@echo off

set "InFile=Test.txt"
set "Word2Replace=Test line 3"
set "Word2ReplaceWith=OOOOOOOOOO"

setlocal EnableDelayedExpansion
for /F "delims=:" %%a in ('findstr /N "%Word2Replace%" "%InFile%"') do set line=%%a
if defined line (
   set /A line-=1
   copy "%InFile%" "tmp.tmp" > NUL
   echo :EOF>> "tmp.tmp"
   call :ProcessFile "!line!" < "tmp.tmp" > "%InFile%"
) else (
   echo Search string not found
)
Del /F /Q "tmp.tmp" 2>nul
goto :EOF

:ProcessFile targetLine
rem Copy lines before target line
for /L %%i in (1,1,%~1) do (
   set line=
   set /P line=
   echo(!line!
)
rem Replace the target line
set /P line=
echo %Word2ReplaceWith%
rem Copy lines after target line
:nextLine
   set line=
   set /P line=
   if "!line:~-4!" equ ":EOF" exit /B
   echo(!line!
goto nextLine


Thanks again,

Post Reply