Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
I've got te following question about batch programmingI've got a txt file
I've got a line in thereexample:
"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 %1For /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 %1but 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

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

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.
For larger files:
M2
If at first you don't succeed, you're about average.

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 IFcause 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 %1changes "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 tochange te line from 2 back to 3 on the next run
so something likeIF "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

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.

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 :eofoke 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 nessecairlyGoto :eof what is EOF
Nathan

It works great
Thank you very muchonly 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 belowFor /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 %1if it is
then run the file belowFor /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 %1But
as you seeEcho 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 doNathan

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.

Oke
but the error level stuff i used as followFind "line number 3" test.txt
goto %errorlevel%:0
test test.txt:1
goto :eof
test2 test.txtand then I used the text replacement stuff
onlyFor /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 %1This 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

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.

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 fileso 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 worksbut 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 formThanks for all the help
It really inspired me to the method i use nowNathan

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.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |