0

I have these 2 files:

test.txt test1.txt

This is the content of the first file:

build.date=220314
build.version=1.0Rel
build.id=TestBuild1.0
build.type=DEV

This is the content of the second one:

build.date=220314
build.version=1.3
build.id=TestBuildRedDot
build.type=DEV

Now. What I want is a batch file that changes the text that comes after "build.id=" (supposing that I don't know what comes after it). Any help?

Mark
  • 3,609
  • 1
  • 22
  • 33

1 Answers1

2

If you don't care about the order of the lines in the file, then you can use FINDSTR to strip out the old build.id line and write the result to a new file. Then append a new line with the new value. Finally move the new file to the original name.

@echo off
>file.txt.new (
  findstr /vbl "build.id=" file.txt
  echo build.id=NewValue
)
move /y file.txt.new file.txt >nul

If you want to preserve the line order, then use FOR /F to read each line, parsing the line into variable name and value. Then a simple IF statement can be used to process the line accordingly.

@echo off
>file.txt.new (
  for /f "tokens=1* delims==" %%A in (file.txt) do (
    if "%%A" == "build.id" (
      echo build.id=NewValue
    ) else (
      echo %%A=%%B
    )
  )
)
move /y file.txt.new file.txt >nul

Or you can make the task really simple by getting your hands on a utility that supports regular expression search and replace. I have written a hybrid JScript/batch utility called REPL.BAT that I find extremely useful. Here is how it could be used for your simple task.

@echo off
type file.txt | repl "^(build\.id=).*" "($1)NewVaue" >file.txt.new
move /y file.txt.new file.txt >nul
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390