(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.
paulie99Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
knightEknightConnect With a Mentor Commented:
The batch variable name isn't so important - you can use what ever you want for that.  Also the search string can also be a variable, but it's value must be exactly specified in order to be search/replaced (obviously).  For example ...

   set  srchStr=full-build-target-project
   set/p XYZ=Enter a value for full-build-target-project:
   chgstr.exe  "%srchStr% = replace this text"  "%srchStr% = %XYZ%"  file1

in the above code the first parameter to chgstr:  "%srchStr% = replace this text"
must evaluate to the exact string to be search/replaced in file1.
0
 
knightEknightCommented:
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.
0
 
BillDLCommented:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
paulie99Author Commented:
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
0
 
paulie99Author Commented:
thanks knightEknight: all points awarded to you. But thanks for the info Bill.
0
 
BillDLCommented:
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
0
 
BillDLCommented:
A couple of little glitches in there with the jump to the :labels, but you'll get the idea.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.