Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#208872 - 11/03/2004 09:51 Batch file question
Phoenix42
veteran

Registered: 21/03/2002
Posts: 1424
Loc: MA but Irish born
Every now and then I have to delve into using batch files to achieve something, sometimes this is from with in windows, sometimes it from a Dos boot floppy.
And every now and then I hit a stumbling block - today’s one involves getting a the contains of a file into a variable, in this case there is only one word in the file, but I'd also like to know how to specify while line of the file I'd like to read - although I'd settle for reading the file line by line till I found a match.
But I digress, in summary how to I get the contains of a file into a variable?

Top
#208873 - 11/03/2004 11:00 Re: Batch file question [Re: Phoenix42]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
If you're using Windows 2000 or XP:

set /P X=Foo < filename
echo %X%

...will display the first line of filename (and leave it in X).
_________________________
-- roger

Top
#208874 - 11/03/2004 12:22 Re: Batch file question [Re: Roger]
Phoenix42
veteran

Registered: 21/03/2002
Posts: 1424
Loc: MA but Irish born
Unforunetly this is from a DOS boot disk.
But I did find an answer.....sometime googling for the obvious helps...I googled on "batch file reading from a file into a variable" (without the quotes) and it return this MS page

DIR C:\ /b | FIND "game" >A:\textfile
COPY init.txt+textfile varset.bat
call varset.bat
c:
cd\%varname%


In this code snipt it is looking for a directory that contins the string "game" and then changes to it.
Though I don't think this code is bullet proof, it does work in the small fish pond that I need it to....I think.

Top
#208875 - 11/03/2004 12:35 Re: Batch file question [Re: Phoenix42]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
You don't supply the contents of init.txt...
_________________________
Tony Fabris

Top
#208876 - 11/03/2004 12:38 Re: Batch file question [Re: Phoenix42]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
I always thought it was interesting that DOS shell scripting requires a command to make calling another program return to the original script after exit whereas Unix scripting requires a command to make it not do that.
_________________________
Bitt Faulk

Top
#208877 - 11/03/2004 12:51 Re: Batch file question [Re: wfaulk]
Roger
carpal tunnel

Registered: 18/01/2000
Posts: 5683
Loc: London, UK
I always thought it was interesting that DOS shell scripting requires a command to make calling another program return to the original script

IIRC, 'call' was only added in DOS 4.0. I guess that stacking the context of the batch interpreter was too much effort. And, yes, it didn't run a fresh shell for each batch file -- they used to be (still are?) run by the current shell instance.

Frankly, if you need to do this kind of thing on Windows, get Cygwin. If you need to do it on DOS, get MKS (if that still exists?).
_________________________
-- roger

Top
#208878 - 11/03/2004 13:30 Re: Batch file question [Re: tfabris]
Phoenix42
veteran

Registered: 21/03/2002
Posts: 1424
Loc: MA but Irish born
Sorry Tony, it on the linked MS page.
init.txt is just
set varname=

There should be no carriage return or linefeed.
To create this file from the command line:
C:\>COPY CON INIT.TXT

SET VARNAME=^Z

I tried creating it on the file from within the batch file by using
echo SET VARNAME=>init.txt
but that creates a CR or LF at the end of the file. This mean that when you later concatenate textfile file onto the end of init.txt and copy them into varset.bat you end up with
set varname=

games
rather then
set varname=games


Sorry long answer.

Top
#208879 - 11/03/2004 13:49 Re: Batch file question [Re: Phoenix42]
tfabris
carpal tunnel

Registered: 20/12/1999
Posts: 31600
Loc: Seattle, WA
Yup, I figured that was what was in init.txt. I do tricks like that all the time to set variables in batch files, but I like Roger's trick a lot better. Didn't know you could do it that way. Very nifty.
_________________________
Tony Fabris

Top
#208880 - 11/03/2004 14:11 Re: Batch file question [Re: tfabris]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
For the record:
a=`cat /etc/passwd`
is all it takes in Unix.
_________________________
Bitt Faulk

Top