Link to home
Start Free TrialLog in
Avatar of paulie99
paulie99

asked on

(MSDOS) user input to replace text in 2 files

I want to promot the user 4 times to replace text in two different files, executed by a MSDOS batch file. The text to replace in one file1 is:
target = replace this text
comment = replace this text

file2:
title = replace this text
version = replace this text

Therefore the parmater:
target = xxxxxx
I want to replace
xxxxxx
but keep
target =
As the values after the = sign are used in the build process. The values after the = sign could be anything as other users will put in text which coukld be incorrect.
Avatar of knightEknight
knightEknight
Flag of United States of America image

Download and unzip this utility:   http://www.paulslore.com/utils/chgstr.zip

Then you can do this in your batch file:

@echo off
setlocal

 set/p target=Enter a value for target:
chgstr.exe  "target = replace this text"  "target = %target%"  file1

set/p comment=Enter a value for comment:
chgstr.exe  "comment = replace this text"  "comment = %comment%"  file1

set/p title=Enter a value for title:
chgstr.exe  "title = replace this text"  "title = %title%"  file2
 
set/p version=Enter a value for version:
chgstr.exe  "version = replace this text"  "version = %version%"  file2

:end



:note that it is important to specify the exact search string (spaces included) in the first parameter
:run chgstr.exe /?  to see the help screen.
Hi Paulie

knightEknight has provided what I think is an excellent method.

If, for some reason, you are forced to only use the program files supplied by Windows and need a "pure" batch file, could you please do two things when responding:
1. Let us know your operating system, or at least a range of perating systems you would be running the batch file within
2. Upload a sample text file.

If done from a batch file using windows/dos program calls only, the usual method is to break lines into chunks using some character as the delimeter, and then replace one of the chunks at the time the line is then redirected out to another file.  The logical choice would be to use the = character as the delimeter, but if the text file is full of = characters, then that is a problem.

Bill
Avatar of paulie99
paulie99

ASKER

Hi all
Unfortunately knightEknight: you have stated that that it is important to specify the exact search string (spaces included) in the first parameter. This I cannot do as in most cases this vale will be a wildcard. That said I suppose I could work around this, I have noticed that if the target is more than one word for example:
 full-build-target-project   = New-release
Then I cannot replace:
set/p t full-build-target-project=Enter a value for full-build-target-project:
chgstr.exe  " full-build-target-project = replace this text"  " full-build-target-project = %full-build-target-project%"  file1
Bill I will be running the batch file on XP, I have attached a sample file.


builds.txt
ASKER CERTIFIED SOLUTION
Avatar of knightEknight
knightEknight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks knightEknight: all points awarded to you. But thanks for the info Bill.
I know this question has been answered, and very well answered at that, but here's another method that you may like to consider either for this scenario of other ones that come up in future.  Maybe the theory applies and maybe it doesn't, but here's my suggestion anyway.

You take the file a line at a time and write it out to another file one line at a time, placing whatever input the user enters into the relevant sections.  For the Yes/No prompts, if the user enters an unexpected value, it prompts repeatedly until it gets it.  For the other variables that are , well "variable", it accepts whatever is input.  The one drawback seems to be that SET /P tries to calculate variables comprising only numeric characters and doesn't treat them as a normal string.

Windows 9x had CHOICE.COM which allowed you to create a menu like this:

          Create a Build Version Number

          1. Yes
          2. No
          Q. Quit

          Enter your selection (1, 2, Q) ...

By using choice.com with lines like this after displaying the menu with "echo" commands:

choice /c:12Q Enter your selection (1, 2, Q) ...
if errorlevel Q goto :END
if errorlevel 2 goto :NUMBER2
if errorlevel 1 goto :NUMBER1

The menu will stay on screen until a matching input is received, and will keep prompting for input.

The workaround for post Win9x is to use SET /P
You can make it display and work in much the same by enforcing an accepted value and prompting until it is received.  That's what I did in the example batch file.

Anyway, just another method to consider.  Test it and see how it works.  Who knows, you may be able to use the theory for your own ends.
Builds-Wizard.txt
A couple of little glitches in there with the jump to the :labels, but you'll get the idea.