(MSDOS) user input to replace text in 2 files

Posted on 2009-04-01
Medium Priority
Last Modified: 2012-05-06
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

title = replace this text
version = replace this text

Therefore the parmater:
target = xxxxxx
I want to replace
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.
Question by:paulie99
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 33

Expert Comment

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

Then you can do this in your batch file:

@echo off

 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


: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.
LVL 38

Expert Comment

ID: 24048520
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.


Author Comment

ID: 24058735
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.

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

LVL 33

Accepted Solution

knightEknight earned 2000 total points
ID: 24059077
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.

Author Closing Comment

ID: 31565250
thanks knightEknight: all points awarded to you. But thanks for the info Bill.
LVL 38

Expert Comment

ID: 24067317
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.
LVL 38

Expert Comment

ID: 24067334
A couple of little glitches in there with the jump to the :labels, but you'll get the idea.

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question