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
Solved

(MSDOS) user input to replace text in 2 files

Posted on 2009-04-01
7
464 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
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
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:BillDL
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.

Bill
0
 

Author Comment

by:paulie99
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.


builds.txt
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 33

Accepted Solution

by:
knightEknight earned 500 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.
0
 

Author Closing Comment

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

Expert Comment

by:BillDL
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.
Builds-Wizard.txt
0
 
LVL 38

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

790 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