Hi all,
I am trying to script a batch for windows to edit a text file in the next manner:
add "Value=2" at the end of a "[Server]" header, and if "Value=*" exists, replace it with the mentioned above.
so that a file that looks like this:
-----------file.txt----------
;comment
;comment
[User]
Bla=a
[Server]
bla=a
Value=1
bla2=b
-----------------------------
will turn out like this:
-----------file.txt----------
;comment
;comment
[User]
bla=a
[Server]
bla=a
bla2=b
Value=2
----------------------------
so far I have gotten close, with:
Quote:
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
for /f %%a in (file.txt) do (
if NOT %%a!==!Value=1 echo %%a >> file.txt.tmp
if %%a==[Server] echo .Value=2 >> file.txt.tmp
if %%a=="" echo.)
del file.txt
rename file.txt.tmp file.txt
that's not really good enough because it only ignores "Value=1" and not all lines starting with "Value=" and it inserts "Value=2" right beneath "[Server]" which is not what I wanted, but the 2 main problems I have are:
1. it, for some reason, ignores the comment lines starting with ";"
2. it ignores newlines, and huddles it all together. (the last line i've written doesn't work to solve that problem... :\)
seems like a pretty simple task, but it's my first text editing batch ever. any scripting masters kind enough to offer some assistance?
thx,
Ron.