Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Line of text from file into environment variable

7 views
Skip to first unread message

William Allen

unread,
Apr 24, 2001, 5:40:15 AM4/24/01
to
===Getting a line of text from a file into an environment variable
A number of recent posts to alt.msdos.batch posed
problems that concerned how to recover a line of text,
written to a file, into an environment variable.
This explains two simple approaches:-

1) Data storage format

In some cases, the file itself is written (often by the
batch script itself) from an environment variable.

If you have a variable:
MYVAR=A string of text

and you want to write it to disk, in order to recover
later into an environment variable, the data format
to use is:

ECHO.SET %%1=%MYVAR%>DATAFILE.BAT
(notice the doubled % in %%1 used to ECHO it literally)

so that DATAFILE.BAT now contains:
SET %1=A string of text

Then to recover the data to, say ANOTHERVAR, all
you need do is use the line:
CALL DATAFILE.BAT ANOTHERVAR

then delete the file DATAFILE.BAT (ideally, that
file should be in your TEMP folder).

Each programming language will tend to have its preferred
data file format or formats. The
SET %1=your data
stored as a temporary .BAT file is a useful format for
batch scripting work.

=====

2) Recovering a line not stored in SET %1= format

The natural extension to the above, when you didn't
(or couldn't) store the data in the SET %1= natural
batch format, is to prefix the line with SET %1=

A simple way to do this is to use DEBUG to create a
file consisting of
SET %1=
with NO CarriageReturn Linefeed combination at the end.
When other text is appended to such a file, it is added
on the _same_ line, completing a simple SET command.

Many people, for no good reason whatsoever, tend to
avoid the use of DEBUG. Yet, among the commands
usable in batch scripts, it has a longer provenance than
many common commands, such as FIND or MOVE,
and it's perhaps the most versatile.

The DEBUG command to enter a data string, such as
SET %1=
is simply
e100'SET %1='

The "e100" says start entering the data following
at location with offset 100 - the standard starting
location for DEBUG (for historical reasons related
to the start offset of the .COM program format).

Data can follow e100 as a simple 'single quote'
string. Notice that the string 'SET %1=' contains
just 7 characters. To set DEBUG up to write this
as a 7 character file, with no CarriageReturn nor
Linefeed added, you need to load the CX register
with 7. You do it with two lines, like this:
rcx
7

(rcx stands for Read/write CX register)

To Write the current file back again, you use the command:
w

To Quit debug, you use the command:
q

So, if you've never used DEBUG, try this in a DOS box in
Win9x or in DOS.

1) Create any file, just so there's one there, like this:
ECHO.Anyfile>FILE.BAT

2) Type the command:
debug file.bat

3) You see the debug prompt, a dash:
-

4) Type the data entry command:
e100'SET %1='

5) Type the read/write CX command:
rcx

(you'll see something like: CX 0009 then
a colon : which means CX is currently
9 = the nine characters in our file, including
its CarriageReturn and Linefeed)

6) At the colon prompt, type 7 (our file length):
7

7) Type the write command:
w

(you'll see something like: Writing 00007 bytes)

8) Type the quit command:
q

Check and you'll see you have a 7 byte file
FILE.BAT
that contains the useful prefix text:
SET %1=
and has no CarriageReturn Linefeed combo at the end.

So you can prefix it to any line of text, say
ECHO.A line of text>TEST.TXT

To append TEST.TXT to FILE.BAT, simply use:
TYPE TEST.TXT>>FILE.BAT

FILE.BAT now reads
SET %1=A line of text

A simple one command batch script that
will load your line of text into any variable
name you call it with.

====Putting the above technique into a batch script

You need to create a file with the debug script:
e100'SET %1='
rcx
7
w
q

Remember, to ECHO a % character, you need
to double it to %% - so do this

ECHO.e100'SET %%1'>SCR.BAT

then use a FOR IN DO statement for the remaining
four lines, appending them to SCR.BAT, like this:

FOR %%V IN (rcx 7 w q) DO ECHO.%%V>>SCR.BAT

Now you have your debug script. Just two lines to create it:
ECHO.e100'SET %%1'>SCR.BAT
FOR %%V IN (rcx 7 w q) DO ECHO.%%V>>SCR.BAT

Debug can run from such a script, as if you'd typed
in the lines yourself, as we did above.

You use the format:
debug FileName < ScriptName

Then debug applies your script in ScriptName to
the file FileName, and writes the result to FileName

If you make both files the same in the above line
they will conflict with each other, the script needs
to be in a separate file. However, you can get round
this by "piping" the script into debug. A pipe operation
creates a temporary file (which it deletes for you) to
do its work. This avoids a conflict, and avoids your
having to create a dummy file first (as we had to with
the Anyfile example when working at the prompt)

So
TYPE SCR.BAT | debug SCR.BAT

sends the script into the command: debug SCR.BAT
and debug rewrites the script file so it becomes the
desired file
SET %1=

To silence the informative output from debug, use >NUL
so that line becomes
TYPE SCR.BAT | debug SCR.BAT>NUL

Now we have just three lines:

ECHO.e100'SET %%1='>SCR.BAT
FOR %%V IN (rcx 7 w q) DO ECHO.%%V>>SCR.BAT
TYPE SCR.BAT | debug SCR.BAT>NUL

Those create your universal prefixing file:
SET %1=
with NO carriage return linefeed

Use it on any line of text in a file, say TEXTFILE.TXT,
like this

TYPE TEXTFILE.TXT>>SCR.BAT
and you have the useful command
SET %1=
prefixed to your line of text.

Just use
CALL SCR.BAT MYVAR
to set MYVAR to the line of text in TEXTFILE.TXT

So the whole operation to recover the text line
in TEXTFILE.TXT becomes

ECHO.e100'SET %%1='>SCR.BAT
FOR %%V IN (rcx 7 w q) DO ECHO.%%V>>SCR.BAT
TYPE SCR.BAT | debug SCR.BAT>NUL
TYPE TEXTFILE.TXT>>SCR.BAT
CALL SCR.BAT MYVAR


All you need to add is
DEL SCR.BAT
to remove the workfile.

Tidy up the code by writing your temporary file
to the TEMP folder, by using %TEMP%.\SCR.BAT
instead of plain SCR.BAT. The code becomes

@ECHO OFF
ECHO.e100'SET %%1='>%TEMP%.\SCR.BAT
FOR %%V IN (rcx 7 w q) DO ECHO.%%V>>%TEMP%.\SCR.BAT
TYPE %TEMP%.\SCR.BAT | debug %TEMP%.\SCR.BAT>NUL
TYPE TEXTFILE.TXT>>%TEMP%.\SCR.BAT
CALL %TEMP%.\SCR.BAT MYVAR
DEL %TEMP%.\SCR.BAT

:: And a line to demo the result
ECHO.%MYVAR%

Other methods exist, but this is simple, general, and useful.

--
William Allen


Benny Pedersen

unread,
Apr 24, 2001, 6:08:42 PM4/24/01
to
"William Allen" wrote
SNIP

> rcx
> 7
> (rcx stands for Read/write CX register)

Yes, and to see what's written, then the DEBUG Command:
d100
or:
d100L10
or just a single d as:
d

Then W and Q...
Benny

0 new messages