[comp.sys.ibm.pc] IBM DOS System Variables

nsto@ur-tut (Natalie Stone) (03/25/88)

Is there a way to assign a variable from within a batch file to be
equal to the current directory name so it can then be used in
the batch file.

creps@silver.bacs.indiana.edu (Steve Creps) (03/25/88)

In article <1501@ur-tut.UUCP> nsto@ur-tut (Natalie Stone) writes:
>Is there a way to assign a variable from within a batch file to be
>equal to the current directory name so it can then be used in
>the batch file.

   Not really with just MS-DOS batch files. It is possible to do from a
high-level language like C, or with assembly language. If you don't want
to program it yourself, there are PD programs available from various places
that do pretty much what you are asking (I assume you want to save the current
directory, then change to another directory, then return to the original
directory). On Unix the commands pushd and popd are used for this purpose.
Pushd saves the current directory, and lets you change directories, whereupon
the popd command will take you back to the original dir. I've seen similar
PC programs called pushdir and popdir which will accomplish this. If requested
I can post them easily, as they take up less than 1K of disk space. On second
thought, I'll do it anyway, so please don't send me requests for them. I don't
want to be more swamped with mail than I already am. Look for them in the
comp.binaries.ibm.pc group soon.

-      -       -       -       -       -       -       -       -	-
Steve Creps on the 8650 runnin' Ultrix at Indiana University.
creps@silver.bacs.indiana.edu (192.12.206.2), ...iuvax!silver!creps,
creps@iubacs.bitnet "Hey fellas, it's a four-legged V-8!"

ftg@gatech.edu (Gary Peterson) (03/25/88)

In article <1501@ur-tut.UUCP>, nsto@ur-tut (Natalie Stone) writes:
> Is there a way to assign a variable from within a batch file to be
> equal to the current directory name so it can then be used in
> the batch file.


Try something like the following:

set tempvar = %prompt%
set prompt = $p             (Should set prompt to pathname,
set pathname = %prompt%      may need a backspace, $b?)
set prompt = %tempvar%

Or since DOS is full of undocumented features, I'm guessing
that just
set pathanme = $p
might work.


ftg@gatech

murillo@sigi.Colorado.EDU (Rodrigo Murillo) (03/26/88)

In <1286@silver.bacs.indiana.edu> creps@silver.UUCP (Steve Creps) writes

>In article <1501@ur-tut.UUCP> nsto@ur-tut (Natalie Stone) writes:
>>Is there a way to assign a variable from within a batch file to be
>>equal to the current directory name so it can then be used in
>>the batch file.
>
>   Not really with just MS-DOS batch files.
>   [ references to pushdir/popdir and sugestions to try a high level language]
Who said it could not be done??
 
Yes folks, batch files are fun!  I devised a way to store the current
directory and return to it from a DOS batch file. It isn't particularly
pretty, but it is handy if you don't have things like popdir/pushdir yet.
 
It also uses some handy features of DOS like redirection and file
concats, and redirection to the NUL: device.
 
Redirection of the CD command to a text file is the key.  CD by itself
returns the current directory. That file is then appended to a 3 character
text file that has the string "CD " in it, WITHOUT a return.  The
resultant file is given a batch extention, and then run to return to
the directory.
 
   create the DOS command file somewhere handy, (no CR)
 
   C:\>copy con \tools\cd.dos
   cd ^Z
     1 File(s) copied
 
   Here is a batch file that goes to the root, then returns
   to whatever directory it started in.
 
   ==> foobar.bat
    echo off
    REM * Redirect the output of cd to a temp file
    cd >\tools\cd.txt
    cd\
    REM * Verify thet we are now in the root
    echo Hello from the root!
    cd
    pause
    REM * Create the go.bat file that will exec the cd\[old directory]
    copy \tools\cd.dos + \tools\cd.txt  go.bat >nul:
    REM * Call the batch file, but return back here.
    REM * DOS 3.3 users can CALL go.
    command/c go
    REM * Drop out of the batch file here, or add more features.
-- 
_______________________________________________________________________________
 Rodrigo Murillo, University of Colorado - Boulder  (303) 761-0410 
 murillo@boulder.colorado.edu | ..{hao|nbires}!boulder!murillo
 ( Machines have less problems.  I'd like to be a machine. -- Andy Warhol )

akk2@ur-tut (Atul Kacker) (03/26/88)

In article <5074@sigi.Colorado.EDU> murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes:
>In <1286@silver.bacs.indiana.edu> creps@silver.UUCP (Steve Creps) writes
>
>>In article <1501@ur-tut.UUCP> nsto@ur-tut (Natalie Stone) writes:
>>>Is there a way to assign a variable from within a batch file to be
>>>equal to the current directory name so it can then be used in
>>>the batch file.
>>
>>   Not really with just MS-DOS batch files.
>>   [ references to pushdir/popdir and sugestions to try a high level language]
>Who said it could not be done??
> 
>Yes folks, batch files are fun!  I devised a way to store the current
>directory and return to it from a DOS batch file. It isn't particularly
>pretty, but it is handy if you don't have things like popdir/pushdir yet.
> 
>     .... stuff deleted .....


I think this is not really what Natalie is after.  It is easy to use pushdir/
popdir or something else to return to the same directory etc.  What she wants
is being able to define an environment variable, called say CURDIR, that would
be equal to the current path.
 e.g. If the batch file is run from C:\USR\BIN, it should set the environment
 variable CURDIR = C:\USR\BIN.

This environment variable could then conceivably be used in another application
program.







-- 
-------------------------------------------------------------------------------
Atul Kacker  |     Internet: akk2@tut.cc.rochester.edu
             |     UUCP: {ames,cmcl2,decvax,rutgers}!rochester!ur-tut!akk2
-------------------------------------------------------------------------------

psfales@ihlpe.ATT.COM (Pete Fales) (03/26/88)

In article <1501@ur-tut.UUCP>, nsto@ur-tut (Natalie Stone) writes:
> Is there a way to assign a variable from within a batch file to be
> equal to the current directory name so it can then be used in
> the batch file.

I would do this with the SETVAR command I obtained from a BBS.  It is
used to set an environment variable from standard input.  So the
following code could be put in your batch file:

cd | setvar PWD
echo I am in directory %PWD%

-- 
Peter Fales		UUCP:	...ihnp4!ihlpe!psfales
			work:	(312) 979-7784
				AT&T Information Systems, IW 1Z-243
				1100 E. Warrenville Rd., IL 60566

murillo@sigi.Colorado.EDU (Rodrigo Murillo) (03/27/88)

In article <17020@gatech.edu> ftg@gatech.edu (Gary Peterson) writes:
>In article <1501@ur-tut.UUCP>, nsto@ur-tut (Natalie Stone) writes:
>> Is there a way to assign a variable from within a batch file to be
>> equal to the current directory name so it can then be used in
>> the batch file.
>
>
>Try something like the following:
>
>set tempvar = %prompt%
>set prompt = $p             (Should set prompt to pathname,
>set pathname = %prompt%      may need a backspace, $b?)
>set prompt = %tempvar%
>
>Or since DOS is full of undocumented features, I'm guessing
>that just
>set pathanme = $p
>might work.

Sorry Gary,  this soulution does not work.  DOS does not do
expansion of $p.  The $p is only meaningful to the PROMPT command.
The result of your solution is that 'prompt' contains the literal
'$p'
 
Also beware of surrounding the equal signs in SET commands with spaces.
The are taken literally and become part of the envar name. So...
 
      set foo=bar  and  set foo = bar
 
create 2 different envars.  And if you write batch files that check
for envar contents, it will barf on one of those.  The convention
is not to include any spaces in the SET arguments.  I've seen many
a batch file crapout because of this,  usually with a puzzled user
on the other end.


-- 
_______________________________________________________________________________
 Rodrigo Murillo, University of Colorado - Boulder  (303) 761-0410 
 murillo@boulder.colorado.edu | ..{hao|nbires}!boulder!murillo
 ( Machines have less problems.  I'd like to be a machine. -- Andy Warhol )

ftg@gatech.edu (Gary Peterson) (03/28/88)

In article <17020@gatech.edu>, ftg@gatech.edu (Gary Peterson) writes:
> In article <1501@ur-tut.UUCP>, nsto@ur-tut (Natalie Stone) writes:
> > Is there a way to assign a variable from within a batch file to be
> > equal to the current directory name so it can then be used in
> > the batch file.
> 
> 
> Try something like the following:
Various stupid stuff deleted
>ftg@gatech

Who is this bozo? Sheesh what a dumb idea.

The following will do the job very simply:

Step 1: create the following one line file:

set pathname=

With NO trailing cr/lf or eof (^Z). If you don't know how to do
this with your editor then do the following in GWBASIC direct mode:

OPEN "PATHNAME.FOO" FOR OUTPUT AS #1
PRINT#1,"set pathname=";
CLOSE
SYSTEM

This creates the file PATHNAME.FOO with a trailing eof.
You get rid of it by a copy:

COPY PATHNAME.FOO /A PATHNAME.1ST /B

Put PATHNAME.1ST somewhere handy, for example your BIN directory
on drive C:. (Delete PATHNAME.FOO.)

Step 2: Create the following BAT file:

@ECHO OFF
COPY C:\BIN\PATHNAME.1ST PATHTEMP.BAT > NUL       (Or wherever you stored it.)
CD >> PATHTEMP.BAT
CALL PATHTEMP
DEL PATHTEMP.BAT

Save this as PATHNAME.BAT in, for example, your BIN directory
where PATHNAME.1ST is.

Step 3: Every time you want to set the Environment Variable
"PATHNAME" to the current path, just type PATHNAME.

Notes: You can remove the last DEL instruction by always
using the same PATHTEMP file, e.g., C:\BIN\PATHTEMP.BAT.
This allows PATHTEMP run as the last instruction of a BAT file
so that no CALL is needed. (For pre-3.3ers.)
You can also add more commands onto the end of PATHTEMP.BAT
using the >> append redirection and TYPE.

The alter ego of ftg@gatech

phil@dhw68k.cts.com (Phil Suematsu) (04/07/88)

In article <17020@gatech.edu> ftg@gatech.edu (Gary Peterson) writes:
>In article <1501@ur-tut.UUCP>, nsto@ur-tut (Natalie Stone) writes:
>> Is there a way to assign a variable from within a batch file to be
>> equal to the current directory name so it can then be used in
>> the batch file.
>
>Try something like the following:
>
>set tempvar = %prompt%
>set prompt = $p             (Should set prompt to pathname,
>set pathname = %prompt%      may need a backspace, $b?)
>set prompt = %tempvar%
>...
>ftg@gatech

Nice try.  You had the right idea.

Here are tested batch files that only use shell commands and COMMAND.COM,
which leave the current directory in the CURRENTDIR environment variable:

For PC-DOS 3.30:
--------------------------------
@echo off
set saveprompt=%prompt%
prompt set currentdir=$p
command/c tmpsetcd>tmpsetcd.bat
prompt %saveprompt%
set saveprompt=
call tmpsetcd
del tmpsetcd.bat
 
For PC-DOS 3.00:
-------------------------------
echo off
set saveprompt= %prompt%
prompt set currentdir=$p
command/c tmpsetcd>tmpsetcd.bat
prompt %saveprompt%
set saveprompt=
tmpsetcd

Note that these batch files work only if you are using a COMMAND.COM that
is NOT patched for turning 'ECHO OFF' automatically.  I couldn't figure out 
how to delete the tmpsetcd.bat file after the PC-DOS 3.00 version.
-- 
_____________________________________________________________
Phil Suematsu | uucp:     ...{trwrb,hplabs}!felix!dhw68k!phil
              | InterNet: phil@dhw68k.cts.com
______________|______________________________________________