View unanswered posts | View active topics It is currently 23 Oct 2014 05:43



Post new topic Reply to topic  [ 5 posts ] 
Find and replace 
Author Message

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


18 May 2012 02:11
Profile

Joined: 10 Feb 2012 02:20
Posts: 4319
Post Re: Find and replace
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


18 May 2012 02:48
Profile

Joined: 18 May 2012 02:03
Posts: 3
Post Re: Find and replace
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.


18 May 2012 07:45
Profile

Joined: 10 Feb 2012 02:20
Posts: 4319
Post Re: Find and replace
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.


18 May 2012 08:22
Profile

Joined: 18 May 2012 02:03
Posts: 3
Post Re: Find and replace
Thanks muchly, that did the trick perfectly. Cheers


18 May 2012 09:11
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 20 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 © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.