3

I'm having problems with a find & replace batch script I've written. I need to be able to open a text file and change some values in it from the windows command line. I can't install sed.exe or FART or powershell so I need to do it this way. It was so much easier to do in linux....

So far I've written this following scrtipt which works perfectly if I'm using a file that only has one word per line, so I can easily change files in the following format

aaa
bbb
ccc

And here's the batch file:

@echo off
setlocal enabledelayedexpansion

if not exist "%1" (echo this file does not exist...)&goto :eof

for /f "tokens=* delims=" %%a in (%1) do (

   set write=%%a
   if "%%a"=="%2" set write=%3

   echo !write! 
   (echo !write!)>>%~n1.replaced%~x1
)

del %1
rename %~n1.replaced%~x1 %1

My problem is that most of the files I need to change consist of lines of multiple values of strings and numbers, like this:

AAA 1.5
BBB 25
CCC 0.1

Can anyone tell me why my script won't change BBB 25 to BBB 45? Is it something to do with the delimiter being a space? I'm pretty sure it has nothing to do with numbers, as if i have a file of words just separated by spaces my script won't work either.

I've been searching through the forums and haven't spotted this problem yet but i'll keep searching. Anyway your help will be greatly appreciated.

UPDATE
Problem Solved!! I found this revision of BatchSubstitute.bat on DosTips.com and just adding an extra echo to a temporary file has solved my problem. I'm still not sure why the code I posted doesn't work so if anyone could explain that it would be great. I'm still quite new to all this!!

Update 2
Here's a typical input file that I need to change from:

k1 1e3
k2 2e5
n 1.5
x 10

And after I run the script (replace.bat prefs.txt "k1 1e3" "k1 5e4") it should be:

k1 5e4
k2 2e5
n 1.5
x 10

It should be able to change any line in the file. The version in the forum in the previous update seems to be able to work, I just have no idea why the first version I posted doesn't work.

jpmorr
  • 500
  • 5
  • 25
  • Haha, thanks. I've actually just tried BatchSubstitute.bat from dostips.com that was mentioned on [another post](http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir). On my windows 7 machine it will display the replaced text, but not overwrite the file, but on the xp machines I need this to work for it doesn't work at all. Need to splice the two files together somehow. – jpmorr Jul 06 '12 at 15:01
  • can you edit your post to include a sample run showing input and output? Or do you just need `...set write "%3"` (note the dbl-quotes). Good luck. – shellter Jul 06 '12 at 15:52
  • It's possibly the double quotes that I'm missing. I'll just go try that now. – jpmorr Jul 06 '12 at 16:09

2 Answers2

1

To answer your update2 question.

It should be able to change any line in the file. The version in the forum in the previous update seems to be able to work, I just have no idea why the first version I posted doesn't work.

It's because the line
if "%%a"=="%2" set write=%3
will not match any of your lines.
As %2 will be expanded to "k1 1e3" so the line will expanded to

if "%%a"==""k1 1e3"" set write="k1 5e4"

A simple tilde should do the trick
if "%%a"=="%~2" set write=%~3

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Actually just hit one more problem - with absolute paths for the file name. If I give a filename of _"C:\Data\prefs.txt"_ when I get to `rename %~n1.replaced%~x1 %1` it just gives back a warning of syntax incorrect AFTER deleting the original file. Is this where I need to put another tilde? – jpmorr Jul 06 '12 at 16:57
  • You need new quotes after you removed them. `ren "%~n1.replaced%~x1" "%~1"` – jeb Jul 06 '12 at 17:08
  • Hi, thanks but it seems that isn't the problem, it relates to the fact that you can move folders when renaming. `RENAME [drive:][path][directoryname1 | filename1] [directoryname2 | filename2] REN [drive:][path][directoryname1 | filename1] [directoryname2 | filename2] Note that you cannot specify a new drive or path for your destination.` As I was creating the temp file in the folder I was running the script it wasn't working so I changed the name and location of my temp file to the full filepath and rename there. Works a charm now. `ren "%~3.replaced%~x3" %~n3%~x3` – jpmorr Jul 07 '12 at 12:17
0

yes, the problem lies with spaces (or quote marks, if you try to keep the text together)

With a command line of test test.txt BBB 25 BBB 45, then %2 is BBB and it wont match the BBB 25 as the 25 is missing

With a command line of test test.txt "BBB 25" "BBB 45", then %2 is "BBB 25" and it wont match the BBB 25 as the quotes are missing.

SeanC
  • 15,695
  • 5
  • 45
  • 66