Computing.Net > Forums > Programming > Batch file for text replacement

Batch file for text replacement

Reply to Message Icon

Original Message
Name: nathan86
Date: June 17, 2005 at 05:22:49 Pacific
Subject: Batch file for text replacement
OS: XP pro
CPU/Ram: 266mhz P2 256 mb ram
Comment:

Hi
I've got te following question about batch programming

I've got a txt file
I've got a line in there

example:

"This line has number 2"

what i want is
that line to become

"This line has number 3"

but
if the line is already

"This line has number 3"

then it should become

"This line has number 2"

I tried to use a batch that i found on this forum

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A%%B>> %1.tmp
If exists "%%A"=="Line number 3"(
Echo Line number 2> %1.tmp))
Move %1.tmp %1

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A%%B>> %1.tmp
If exists "%%A"=="Line number 2"(
Echo Line number 3> %1.tmp))
Move %1.tmp %1

but it does 2 things wrong
1. it removes al the text above the line i replace (that's not a problem cause i edit the first line)
2. I can't seem to get it to replace the text so that if it's a 2 it wil become a 3 and backwards
(I defined the whole line because there are multiple 2 and/or 3's in my txt)

Nathan


Report Offensive Message For Removal

Response Number 1
Name: nathan86
Date: June 17, 2005 at 05:25:33 Pacific
Subject: Batch file for text replacement
Reply: (edit)

b.t.w
the exists after the if
doesn't belong there
it was just a test but it didn't function the way it should.

Nathan


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: June 17, 2005 at 06:05:28 Pacific
Subject: Batch file for text replacement
Reply: (edit)

Hi Nathan,

IVO can probably make it work; but not me.

One "solution" is to use CHANGE.COM which will work on any text file up to about 32KB.

Usage: change.com "stringold" "stringnew"

Note: EXACTLY one space between double quoted strings.

Change.com

For larger files:

Change9.11

M2


If at first you don't succeed, you're about average.


Report Offensive Follow Up For Removal

Response Number 3
Name: nathan86
Date: June 17, 2005 at 07:25:17 Pacific
Subject: Batch file for text replacement
Reply: (edit)

I see
But change.com only changes te string on request and I allready managed to change the string with te above batch
but i'm more worried about how i should setup my IF

cause when the value is allready changed to "stringnew" and I run the batch again it should change it back to string old

but that's i can't figure out how

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A%%B>> %1.tmp
If "%%A"=="Line number 3"(
Echo Line number 2> %1.tmp))
Move %1.tmp %1

changes "Line number 3" to "Line number 2"
but if I opened the batch and the line has changed from 3 to 2 I want the batchfile to

change te line from 2 back to 3 on the next run
so something like

IF "line number 2" EXISTS
CHANGE TO "line number 3"
IF "Line number 3" EXISTS
CHANGE TO "line number 2"

only i don't know how to define this action in batch

Nathan


Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: June 17, 2005 at 07:42:40 Pacific
Subject: Batch file for text replacement
Reply: (edit)

find "Line number 3" textfile
goto %errorlevel%

:0
It's there
goto :eof

:1
it's not there
do your change
goto :eof

...
and so on


M2


If at first you don't succeed, you're about average.


Report Offensive Follow Up For Removal

Response Number 5
Name: nathan86
Date: June 20, 2005 at 01:25:45 Pacific
Subject: Batch file for text replacement
Reply: (edit)

I think i don't completely get it

find "Line number 3" textfile
goto %errorlevel%

here you say

Find "line number 3" test.txt

oke that sounds logical

then you come with

goto %errorlevel%

Why is the errorlevel and what does it do

:0
It's there
goto :eof


:1
it's not there
do your change
goto :eof

oke here I ran in a goto

does the :1 have a funtion
(how can i define that it's not there)
or is that not nessecairly

Goto :eof what is EOF

Nathan


Report Offensive Follow Up For Removal


Response Number 6
Name: nathan86
Date: June 20, 2005 at 01:54:22 Pacific
Subject: Batch file for text replacement
Reply: (edit)

It works great
Thank you very much

only one little question if you don't mind

I used it the following way
if the text isn't in the file
then run the file below

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A%%B>> %1.tmp
If "%%A"=="Line number 3"(
Echo Line number 2> %1.tmp))
Move %1.tmp %1

if it is
then run the file below

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A%%B>> %1.tmp
If "%%A"=="Line number 2"(
Echo Line number 3> %1.tmp))
Move %1.tmp %1

But
as you see

Echo line number3> %1.tmp

the > define's that it should replace text
but it replaces the all the text in the file
and that is something it should not do

Nathan


Report Offensive Follow Up For Removal

Response Number 7
Name: Mechanix2Go
Date: June 20, 2005 at 19:55:12 Pacific
Subject: Batch file for text replacement
Reply: (edit)

Hi Nathan,

Yes, that's why I could not see how that code would work.

As to the errorlevel.

ERRORLEVEL [a dumb name] is set by many programs to indicate the result of their action.

errorlevel is available to other programs. In NT there is a built in environment variable %errorlevel% so you can "GOTO %errorlevel%

With FIND errorlevel is set to 0 if the string is found and set to 1 or higher otherwise.

In this case all we need to know is whether the string is there or not.

HTH

M2


If at first you don't succeed, you're about average.


Report Offensive Follow Up For Removal

Response Number 8
Name: nathan86
Date: June 21, 2005 at 00:28:26 Pacific
Subject: Batch file for text replacement
Reply: (edit)

Oke
but the error level stuff i used as follow

Find "line number 3" test.txt
goto %errorlevel%

:0
test test.txt

:1
goto :eof
test2 test.txt

and then I used the text replacement stuff
only

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A%%B>> %1.tmp
If "%%A"=="Line number 2"(
Echo Line number 3> %1.tmp))
Move %1.tmp %1

This line replaces but removes all that's above the replaced text

Echo Line number 3> %1.tmp))

If I change it to this

Echo Line number 3>> %1.tmp))

It adds it to my txt file without removing anything

isn't it just possible to make it only replace the line I want it to replace and keep the rest of the txt file in it's original state???


Nathan


Report Offensive Follow Up For Removal

Response Number 9
Name: Mechanix2Go
Date: June 21, 2005 at 01:09:32 Pacific
Subject: Batch file for text replacement
Reply: (edit)

This:

Echo Line number 3> %1.tmp

will wipe out the file, leaving only what you just ECHOed in.

This:

Echo Line number 3>> %1.tmp

will append the ECHOed string to the end of the exisying file.

So neither is really satisfactory for "replacing" a string except in special cases. For instance, where the string is at the beginning or end on a line by itself.

What is the function of this ?:

"tokens=1,2 delims=="


M2


If at first you don't succeed, you're about average.


Report Offensive Follow Up For Removal

Response Number 10
Name: nathan86
Date: June 21, 2005 at 06:34:22 Pacific
Subject: Batch file for text replacement
Reply: (edit)

Well

I've got config files for some network management tool

only after a server restart the port nr of the server changes so my monitoring tool get's no values back from SNMP
i thougt a small addaption in the config file (port nr) could be easely done with batch but when I used the above script it flushes out the whole file

so i've got 2 options left I guess

1: Find a batch method to replace only the nessecairly string

2: make 4 text files with all possible combinations of the string and write it over the old file when the batch is run
picking the right one depending on what the current string is.


(The last one worked for me by the way)

oke it got as big as 10 batch files triggering each other
but it works

but that doesn't mean i'm not still interested in how you can replace a single string using batch

so plz if you know or can find out how it's done
reply to the form

Thanks for all the help
It really inspired me to the method i use now

Nathan


Report Offensive Follow Up For Removal

Response Number 11
Name: Mechanix2Go
Date: June 22, 2005 at 00:07:20 Pacific
Subject: Batch file for text replacement
Reply: (edit)

Hi Nathan,

No, I don't know how to replace a string [or even a line] with a BAT. If I figure it out I'll post it in this forum.

I use CHANGE.COM to do a similar task. I have an HTML file which redirects to my home [dynamic IP] server. I CHANGE the HTML to the current IP.

Good Luck

M2


If at first you don't succeed, you're about average.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch file for text replacement

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software