find and replace words *

December 12, 2009 at 08:35:52
Specs: Windows Vista
Hi, I was googling around and it returned me two topics from this forum... Since that, I'm trying to do one little .bat to find and replace a setence without removing the blank lines from the file.

The topics:

I wanted it to ask for the file, then ask for that setence and replace (keeping blank lines) :)
- I failed while trying to do such bat (specially because of blank lines)

Can you guys help me?

See More: find and replace words *

Report •

December 12, 2009 at 23:35:07
The one from Feb 09 shows the way.

@echo off > newfile
setLocal EnableDelayedExpansion

set /p file=which file ? :
set /p old=old string ? :
set /p new=new string ? :

for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< %file%') do (
set str=%%b
if not "%%b"=="" (
set str=!str:%old%=%new%!
echo.!str!>> newfile
) else (
>> newfile echo.

Helping others achieve escape felicity


Report •

December 12, 2009 at 23:38:18
I'm just regurgitating what you saw in the original posts, but here it is (not tested):

@echo off > newfile && setlocal
set /p xx=replace sentence:
set /p yy=with:
for /f "tokens=1* delims=]" %%a in ('find /v /n "" ^< infile.txt') do (
if %%b equ %xx% (echo.%yy% >> newfile) else (echo.%%b >> newfile)

(oops, sorry M2, i keep stepping into your posts, my computer barfed and i had to reboot and in the meantime you were here...)

Report •

December 14, 2009 at 08:36:17
Thanks for your answers :)
M2's code is working, but nbrane's one isn't...

It's going to be very useful ((:

Report •

Related Solutions

December 14, 2009 at 14:57:31
somehow that doesn't suprise me! LOL
since my code bombed i though i would experiment:
two versions, one "tags" the end of file, the other counts the
lines which is the one i would use, if any.

:tag the eof
@echo off > newfile && setlocal
set endofil=this_marks_the_end_of_file
echo %endofil%>> infile.txt
set /p aa=replace:
set /p bb=with:
set aa="%aa%"
call :switch < infile.txt
goto :EOF

set xx=
set /p xx=
if "%xx%" equ "%endofil%" goto :EOF
if "%xx%" equ %aa% set xx=%bb%
echo.%xx% >> newfile
goto :switch

:count the lines
@echo off > newfile && setlocal
for /f %%z in ('find /v /c "" ^< infile.txt') do (set lc=%%z)
set /p aa=replace:
set /p bb=with:
set aa="%aa%"
call :switch < infile.txt
goto :EOF

for /L %%n in (1 1 %lc%) do (
call :getline
goto :EOF

set xx=
set /p xx=
if "%xx%" equ %aa% set xx=%bb%
echo.%xx% >> newfile

Report •

December 18, 2009 at 18:19:20
Thanks for trying to help (:
(even if the first code in the topic already worked)

And i'm able to test any codes :]

But I still got errors when running your .bat
Both (tag eof and counting lines) with same error, maybe it's something here...

I got this:
'OFF]' is not recognized as an internal or external command, operable program or batch file

edit: and the 'newfile' erased everything after the word I typed at 'replace' the first blank line.

so, what's wrong here?

Report •

December 18, 2009 at 18:58:15
i don't know, maybe wiser heads than mine can see the flaw.
it worked at my end, i even threw in %aa%, %xx% and %bb% to try and make it crash and it still worked. you ARE running vista however, and I am using xp pro. my scripts are saved as .bat not .cmd (i wish someone would explain the essential difference (hint..hint)) I ran both versions and both checked out ok. what is your test data (assuming it's brief)and what line did you tell it to replace?

Report •

December 18, 2009 at 21:02:27
Hi nbrane,

Trying to follow your script makes my head hurt. I did notice you have two :switch labels, which may cause problems.

(i wish someone would explain the essential difference (hint..hint))

As far as execution goes they are identical. I believe .cmd was intended for scripts that won't run on dos/win9x, whilst leaving .bat available for scripts that are compatible with all systems.

In the end it offers no more than a mechanism to sort scripts and avoid execution or maintain compatibility, depending on the extension.

Batch Variable how to

Report •

December 18, 2009 at 21:16:18
thanks for your help judago, now i won't have to worry about if my script saved .bat or .cmd (I'm old-school, .bat(s) in my belfry!)
The two versions of the script were globbed into one post, poss. source of confusion. they are meant to be separate scripts. each has only only one call to :switch. I should have clarified that. put a <snip> between the blank line and the (non-label) ":count the lines". apologies
(working on improving 'rems' vs labels, using :: for rems now)
(for that matter, maybe i should just use 'rem'? )
ps: (edit) the OFF] looks suspiciously like an ECHO reverb, "ECHO IS OFF" but no clue...

Report •

December 18, 2009 at 21:29:05
:: is normally fine but I'm pretty sure that if inside a code block only rem works.

That's just from memory, I can't test at the moment.

Batch Variable how to

Report •

December 19, 2009 at 12:27:12
I'm not running vista here, actually it's XP. :)

the 'OFF]' may come from the file i'm using...
the file I used to test is:
echo /? > infile.txt

and it has the following line:

but any word I try to replace I got the same error and the same newfile-result (which contains only the first line).

I tried with another file (rd /? > infile.txt)
But it didn't replace anything... :/

Report •

December 19, 2009 at 14:22:27
yes, that nailed it! thanks, i'm learning a lot from this kind of problem. the ECHO | (echo followed anywhere after by the pipe symbol) causes the echo to be expanded and only the data after the pipe (including the pipe) gets saved in the prompt-variable. this behaviour is circumvented by use of
enabledelayedexpansion. but with enab not set. the SET /P
variable retained the pipe as its first char. and everything in front of the pipe disappeared into limbo. then if you try to echo the variable, the variable activates as a command:
@echo off && setlocal
set /p xx=enter:
echo %xx%

now, if I enter the following: testing | type foo.txt
at the "enter:" prompt, %xx% becomes: "| type foo.txt"
now if i try to echo the value of %xx%, it looks (to the script interpreter) like: echo | type foo.txt and so %xx% becomes non-displayable via "echo". the only way to display it is to remove the pipe: set xx=%xx:~1% and now it can be echoed.
while this is a minor irritation, what if your user, at a set /p, enters: testing > valdata.xls
eh? there goes your spreadsheet you've been working on for three months!

well, i enabled delayed expansion, and i'll just repost the new code complete to avoid confusion trying to "patch" the original version.
@echo off && setlocal enabledelayedexpansion
for /f %%z in ('find /v /c "" ^< infile.txt') do (set lc=%%z)
set /p aa=replace:
set /p bb=with:
call :getline < infile.txt > newfile
goto :EOF

for /L %%n in (1 1 %lc%) do (
set xx=
set /p xx=
if "!xx!" equ "!aa!" set xx=%bb%

this worked by "defeating" the pipe symbol that was crashing the original version. this shows yet another reason to use enabledelayedexpansion, if a user includes a pipe anywhere in his response to a "set /p", the batch will blow up.
ps: stumbled on another way to capture blank lines, just use 'type filename' like:
for /f "tokens=* delims=" %%a in ('type "%file%"') do echo line is [%%a]

Report •

Ask Question