Solved

(MSDOS) user input to replace text in 2 files

Posted on 2009-04-01
7
456 Views
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

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.
0
Comment
Question by:paulie99
  • 3
  • 2
  • 2
7 Comments
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
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
 

Author Comment

by:paulie99
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:paulie99
Comment Utility
thanks knightEknight: all points awarded to you. But thanks for the info Bill.
0
 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
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
 
LVL 38

Expert Comment

by:Insignificant Volunteer
Comment Utility
A couple of little glitches in there with the jump to the :labels, but you'll get the idea.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now