Find and replace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
imac1971
Posts: 3
Joined: 18 May 2012 02:03

Find and replace

#1 Post by imac1971 » 18 May 2012 02:11

hi,

I'm using the find and replace script as described on the site but i want the output piped directly to a file within the script and not on the command line and unsure where in the script to pipe it out. I have set the parameters within the script so am just calling the script name. ie.
@echo off


set file=c:\jobs\_IAT\20120512_ASAPRES_00007.txt
set newstr=0000007Advice
set oldstr=0000000Advice

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed

for /f "tokens=1,* delims=]" %%A in ('"type %file%|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%oldstr%=%newstr%%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)

Any direction would be appreciated.

Thanks

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

Re: Find and replace

#2 Post by foxidrive » 18 May 2012 02:48

Code: Select all

@echo off

set "file=c:\jobs\_IAT\20120512_ASAPRES_00007.txt"
set "newfile=c:\jobs\_IAT\20120512_ASAPRES_00007 version 2.txt"
set "newstr=0000007Advice"
set "oldstr=0000000Advice"

echo> tmp.vbs s = Wscript.StdIn.ReadAll
echo>> tmp.vbs Wscript.Echo Replace(s,"%oldstr%","%newstr%")

type "%file%" |cscript /nologo tmp.vbs >"%newfile%"
del tmp.vbs

imac1971
Posts: 3
Joined: 18 May 2012 02:03

Re: Find and replace

#3 Post by imac1971 » 18 May 2012 07:45

Thanks for that, only it is a vb script and i would use it if i could, however the script that is mentioned is part of a larger script, so i really need to echo out to a separate file within the above script, but thanks again.

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

Re: Find and replace

#4 Post by foxidrive » 18 May 2012 08:22

OK. Try this:


for /f "tokens=1,* delims=]" %%A in ('"type %file%|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%oldstr%=%newstr%%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do >>"newfile.txt" %%~X
) ELSE echo.
)


There seem to be an awful lot of " characters that shouldn't be there.

imac1971
Posts: 3
Joined: 18 May 2012 02:03

Re: Find and replace

#5 Post by imac1971 » 18 May 2012 09:11

Thanks muchly, that did the trick perfectly. Cheers

Post Reply