0

I am not very good at batch scripting which is why I need help with a task as simple as this.

What I want to do is to scan a file, look for a line matching a specific pattern (need not be regexp) and when it is found, change it.

The line I'm looking for looks like this:

<ApplicationVersion>1.29.586.5771</ApplicationVersion>

And I want to change it to this:

<ApplicationVersion>1.31.633.6832</ApplicationVersion>

Of course, the numbers may be something else. Is there a nice way of doing this in batch without changing anything else in the file?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Christoffer Reijer
  • 1,925
  • 2
  • 21
  • 40
  • Can you use a regexp tool, or did you mean you don't want regexp? – foxidrive Jan 25 '14 at 10:26
  • [This](http://stackoverflow.com/questions/20979763/how-to-replace-strings-in-windows-batch-file/20984141#2098414) should be quite easy to edit to do what you like. Let me know of you need a hand. – unclemeat Jan 25 '14 at 10:51
  • possible duplicate of [How can you find and replace text in a file using the Windows command-line environment?](http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir) – dbenham Jan 25 '14 at 14:50

2 Answers2

1

Install the Find And Replace Text command line utility and then you can simply enter..

fart yourfile.txt 1.29.586.5771 1.31.633.6832
Sunny
  • 7,812
  • 10
  • 34
  • 48
0

This works for you and will also preserve the empty lines in your input file.....

@Echo OFF

REM Set These Variables
SET "InFile=InputFile.txt"
SET "OutFile=Outputfile.txt"
SET "Replace=1.29.586.5771"
SET "ReplaceWith=1.31.633.6832"

REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"

REM Create The OutputFile with changes
SETLOCAL EnableDelayedExpansion
<"!InFile!" (
FOR /L %%a IN (1 1 0) DO SET /p "="
FOR /L %%A IN (1 1 %Till%) DO (
SET "line="
SET /P "line="
IF "!line!x" == "x" ( Echo.
) ELSE ( Echo !line:%Replace%=%ReplaceWith%!)
)
)>>"%OutFile%"
ENDLOCAL
Sunny
  • 7,812
  • 10
  • 34
  • 48