Click here to Skip to main content
12,404,895 members (67,725 online)
Rate this:
 
Please Sign up or sign in to vote.
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.
Posted 27-Jan-13 0:14am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try this, it's far from perfect but seems to do most of what you want.
@echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
 
set val2="N"
for /f "eol=^" %%a in (file.txt) do (
if NOT %%a!==!Value=1 echo %%a >> file.txt.tmp
if %%a==[Server] set val2="Y"
if %%a=="" echo. >> file.txt.tmp
)
if %val2% == "Y" echo Value=2 >> file.txt.tmp
del file.txt
rename file.txt.tmp file.txt
The "eol=^" stops the parser from treating comment lines (i.e. those beginning with a semi-colon) as blanks. I think the line if %%a=="" echo. >> file.txt.tmp is redundant as the file parser does not return items in blank or comment lines.
  Permalink  
Comments
Ron Anoshi 27-Jan-13 7:23am
   
thanks for the reply.

1. "eol=^" did some good, but some comment rows have a space right after the ";" (I should have been more specific), so now i get something like:
-------file.txt-------
;
;
;comment
----------------------

2. the line is redundant. as i said, it was an attempt to insert a newline which doesn't work. it didn't work in your example either :(
Richard MacCutchan 27-Jan-13 7:37am
   
1. This is the result that I got with my script (no extra comment lines):

;comment
;comment
[User]
Bla=a
[Server]
bla=a
bla2=b
Value=2


2. There is no way to identify blank lines as the parser does not return anything for them. You would need to use some other variable (as I have done with val2) to identify when to add them.
Ron Anoshi 27-Jan-13 7:56am
   
1. your result is correct, but what do you do with a line like:
; comment
(with a space)? that's what i meant...

2. ok, so here's an idea - blank lines come only before a [] header. any ideas on how to identify something of the kind "[Headed]" and how to insert a blank line before it?

much thanks anyway,
ron.
Richard MacCutchan 27-Jan-13 9:05am
   
1. Add the text delims=^ to the options string in the FOR command so it returns the entire line as the token.

2. I have not managed to figure this one as it would need some way of checking for a substring.
Ron Anoshi 30-Jan-13 3:06am
   
ok, I eventually wrote it in cpp, because it turns out there's an easy library for that stuff (it's an .ini file), but it was a good tryout and thanks for your help!

ron.
Richard MacCutchan 30-Jan-13 3:31am
   
I was going to suggest that, but I thought you may have had a specific reason why you had to use the command processor. Anyway, it was a useful exercise for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100