Click here to Skip to main content
15,868,016 members

text editing batch file ignors newlines and rows starting with ";"

Ron Anoshi asked:

Open original thread
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.
Tags: Windows, String, Batch, Script

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900