Batch file question

Posted by: Phoenix42

Batch file question - 11/03/2004 09:51

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?
Posted by: Roger

Re: Batch file question - 11/03/2004 11:00

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).
Posted by: Phoenix42

Re: Batch file question - 11/03/2004 12:22

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.
Posted by: tfabris

Re: Batch file question - 11/03/2004 12:35

You don't supply the contents of init.txt...
Posted by: wfaulk

Re: Batch file question - 11/03/2004 12:38

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.
Posted by: Roger

Re: Batch file question - 11/03/2004 12:51

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?).
Posted by: Phoenix42

Re: Batch file question - 11/03/2004 13:30

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.
Posted by: tfabris

Re: Batch file question - 11/03/2004 13:49

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.
Posted by: wfaulk

Re: Batch file question - 11/03/2004 14:11

For the record:
a=`cat /etc/passwd`
is all it takes in Unix.