DosTips.com

A Forum all about DOS Batch
It is currently 25 Sep 2016 02:48

All times are UTC-06:00




Post new topic  Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Find and replace
PostPosted: 18 May 2012 02:11 
Offline

Joined: 18 May 2012 02:03
Posts: 3
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


Top
   
 Post subject: Re: Find and replace
PostPosted: 18 May 2012 02:48 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5950
Code:
@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


Top
   
 Post subject: Re: Find and replace
PostPosted: 18 May 2012 07:45 
Offline

Joined: 18 May 2012 02:03
Posts: 3
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.


Top
   
 Post subject: Re: Find and replace
PostPosted: 18 May 2012 08:22 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5950
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.


Top
   
 Post subject: Re: Find and replace
PostPosted: 18 May 2012 09:11 
Offline

Joined: 18 May 2012 02:03
Posts: 3
Thanks muchly, that did the trick perfectly. Cheers


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 5 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited