[comp.sys.ibm.pc] Help with directories and batch files...

m-liu@flims (Micky Liu) (03/17/87)

I am trying to use a batch file to:

1) save the current directory
2) goto another directory
3) execute some program
4) return to saved directory

I'm having a great deal of trouble trying to get PC-DOS 3.1 to do this.  In
Unix it would look like:

OLDDIR=`pwd`
cd \anywhere\i\have\to\go
program-that-i-want-to-execute
cd $OLDDIR

Anybody out there have any idea how I can do this?  Step one is the problem
here.  In the manual it says that DOS remembers the original directory, but
it doesn't return to the original directory at the end of the batch file.  I
tried to write a program to put something in the environment, but that is no
good since when the program terminates the newly modified environment goes
with it...

Thanking you all in advance!!!

Micky Liu

  ARPA: x1.micky@cu20b.columbia.edu
  UUCP: ...seismo!columbia!cu20b.columbia.edu!x1.micky
        ...seismo!columbia!flims!m-liu
BITNET: malua@cuvmc

madd@bucsb.bu.edu.UUCP (Jim "Jack" Frost) (03/18/87)

In article <4471@columbia.UUCP> x1.micky@cu20b.columbia.edu (Micky Liu) writes:
>I am trying to use a batch file to:
>
>1) save the current directory
>2) goto another directory
>3) execute some program
>4) return to saved directory
>
>I'm having a great deal of trouble trying to get PC-DOS 3.1 to do this.
[...]
>Anybody out there have any idea how I can do this?  Step one is the problem
>here.  In the manual it says that DOS remembers the original directory, but
>it doesn't return to the original directory at the end of the batch file.  I
>tried to write a program to put something in the environment, but that is no
>good since when the program terminates the newly modified environment goes
>with it...

There's really no easy way to do this (ie, DOS gives no function that
can do it).  What you need to do is either get hold of some program
that inplements directory stacks (there's a pd program pair pushdir
and popdir that would work) or to write a program to save the old
directory into a file (or write a simple shell program, which is more
involved).  If you can get hold of pushdir/popdir, you can do the
following batch file:

  rem save the old dir and switch to the new dir
  pushdir \newdir\that\you\want
  rem run program
  program
  rem return to old directory
  popdir

You can write a real simple pushdir/popdir pair yourself by saving the
current directory in a file and having popdir retrieve it.  The
programs are simple enough to write that I leave it as an exercise to
the reader.  Besides, I have no idea what kind of languages you use.

The fastest method (execution wise) is to create a shell program that
gets the current directory, saves it in a string, does a chdir to the
new directory, exec's the program you want to run, then does a chdir
back when the program gives control back.  This takes memory, however
(although only a few bytes if you write it in assembler).

If you want pushdir/popdir, you should first try asking friends.  A
surprising number of people have these.  If nobody does, then look
around the larger bbs's.  They probably will (I've seen it on several
-- one of them is a bbs sponsored by Systems House in Laconia NH,
phone (603) 524-1000, which just happens to be the only one that I
know the number of off the top of my head).  These programs are real
short, so they don't take too much time to download.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                   - Jim Frost * The Madd Hacker -
UUCP:  ..!harvard!bu-cs!bucsb!madd | ARPANET: madd@bucsb.bu.edu
CSNET: madd%bucsb@bu-cs            | BITNET:  cscc71c@bostonu
-----------------------------------+-----+------------------------------
"New minds, fresh ideas -- be tolerant." | _@o.  <- Mick the Micro Mouse

keast@bucsb.bu.edu.UUCP (Kenneth East) (03/18/87)

In article <838@bucsb.bu.edu.UUCP> madd@bucsb.bu.edu.UUCP (Jim "Jack" Frost) writes:
>In article <4471@columbia.UUCP> x1.micky@cu20b.columbia.edu (Micky Liu) writes:
>>I am trying to use a batch file to:
>>
>>1) save the current directory
>>2) goto another directory
>>3) execute some program
>>4) return to saved directory
[...]
>>Anybody out there have any idea how I can do this?  Step one is the problem
[...]
>
>There's really no easy way to do this (ie, DOS gives no function that
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Actually there is a very simple way to do it with I/O re-direction:

1)  Create a file called cd.txt which contains ONLY the two letters
    'cd' followed by a space followed by a ^Z (end of file).  Let's say
    that we have created such a file and placed it in the
    directory \batch.

2)  You will have to place the following in a batch file which
    in turn will execute your program(s).  For example:

    REM place a 'cd ' in the file old_dir.bat
    copy \batch\cd.txt \batch\old_dir.bat
    REM append the current directory path to the same file
    cd >> \batch\old_dir.bat
    REM now you can change directories at will and execute programs, etc.
    cd \progs\etc
    foo.exe
    REM to return to the original directory, just execute the batch file
    REM you created above:
    \batch\old_dir.bat
    REM and you are back at the original directory.

Please, no flames.  I know it's inelegant, but it works fine, and I have
better things to do than writing a "real" program to do this.  -- 
======================================+=================================
UUCP:  ..!harvard!bu-cs!bucsf!keast   | ARPANET: keast@bucsf.bu.edu
CSNET: keast%bucsf@bu-cs              | Kenneth East @ Boston University
======================================+================================= 

bobmon@iuvax.UUCP (Che Flamingo) (03/18/87)

x1.micky@cu20b.columbia.edu (Micky Liu) writes:
>
>I am trying to use a batch file to:
>
>1) save the current directory
>2) goto another directory
>3) execute some program
>4) return to saved directory

I got a couple of dandy programs off the net, called "pushd.exe" and "popd.exe",
that do exactly this by "pushing" to some directory (saving the current dir in
an environment variable), then "popping" back to the original.

Um...actually, it's not perfect.  It doesn't work in secondary copies of 
COMMAND.COM -- can't find the current environment to store in/read from, I
think.  Overall, though, I'm quite pleased with it.

perkins@bnrmtv.UUCP (Henry Perkins) (03/18/87)

> I am trying to use a batch file to:
> 
> 1) save the current directory
> 2) goto another directory
> 3) execute some program
> 4) return to saved directory
>
> Micky Liu   ...seismo!columbia!flims!m-liu

Create a file called CDSTRING on a path I'll represent as
diskname:\cdstringpath\CDSTRING which contains only this text, not
including the quotes: "CD "

Then your batch can look like the following:

    COPY diskname:\cdstringpath\CDSTRING diskname:\popdirpath\POPDIR.BAT
    CD >> diskname:\POPDIR.BAT
    CD diskname:\whatever\path\you\need
    progname
    diskname:\popdirpath\POPDIR

On DOS versions before 3.0, you'd need to have diskname:\popdirpath
in your search path, and remove it from in front of the POPDIR in the
last line.
-- 
{hplabs,amdahl,3comvax}!bnrmtv!perkins        --Henry Perkins

It is better never to have been born.  But who among us has such luck?
One in a million, perhaps.

scott@ubvax.UUCP (Scott Scheiman) (03/18/87)

In article <4471@columbia.UUCP>, m-liu@flims (Micky Liu) writes:
> 
> I am trying to use a batch file to:
> 
> 1) save the current directory
> 2) goto another directory
> 3) execute some program
> 4) return to saved directory

This may be a little kludgey for you, but I've been able to do this
using just batch files (hack, hack!).  Here's my PUSHD.BAT:

   echo off
   copy c:\bat\pushd.dat d:\~push.bat >nul:
   cd >>d:\~push.bat
   echo set ~PUSH=%%PUSH%% %%~PUSH%%>>d:\~push.bat
   echo set PUSH=>>d:\~push.bat
   if not "%1"=="" echo %1 %2 %3 %4 %5 %6 %7 %8 %9 >>d:\~push.bat
   d:\~push

where the file pushd.dat contains:

   echo off
   set PUSH=

What all this does is create a temporary batch file ~push.bat in a
work directory (here it's d:) which places your current directory name
into the environment string PUSH, then moves it to the front of a string
of saved directories in the environment string ~PUSH (and removes PUSH).
If pushd was called with arguments, they are placed in ~push.bat to be
executed.  Finally, pushd.bat chains to the newly-created ~push.bat.

What this relies on is (1) batch-file chaining and (2) the %-notation
substitution into batch files from the environment which was much
discussed on the net a few weeks ago.  In order to do the four steps you
wanted, chaining is necessary:  your original command (presumably a
batch file) chains to pushd.bat, then ~push.bat, which then runs another
batch file (unless you modify my stuff to do it) to change the
directory, run your command, and finally chain to popd.bat.  Regarding
the % substitutions, note that the %'s are doubled to defer the
substitution until ~push.bat runs.

In order for this to work, pushd.dat must NOT end in CR-LF.  I don't
know of any editors which allow this fully, so I've used the Norton
Utility (or DEBUG) to change the CR-LF to blanks.

For completeness, here's my popd.bat.  I'll leave it's analysis as an
exercise for you readers:

   echo off
   if exist d:\~push.bat erase d:\~push.bat
   if "%1"=="" c:\bat\~popd %~PUSH%
   chdir %~PUSH%
   if "%1"=="/" set ~PUSH=
   if "%1"=="/" shift
   if "%1"=="." shift
;  %1 %2 %3 %4 %5 %6 %7 %8 %9

where ~popd.bat is:

   echo off
   if "%1"=="" goto finis
   chdir %1
:finis
   set ~PUSH=
   if not "%2"=="" set ~PUSH=%2 %3 %4 %5 %6 %7 %8 %9
-- 
"Ribbit!"     Scott (Beam Me Up, Scotty!) Scheiman   Industrial Networking Inc.
  `/\/@\/@\/\       ..decvax!amd!ubvax!scott           3990 Freedom Circle
  _\ \ -  / /_           (408) 562-5572                  Santa Clara, CA 95050

chassin@rpics.RPI.EDU (Dave Chassin) (03/19/87)

In article <839@bucsb.bu.edu.UUCP>, keast@bucsb.bu.edu.UUCP (Kenneth East) writes:
> >>I am trying to use a batch file to:
> >>
> >>1) save the current directory
> >>2) goto another directory
> >>3) execute some program
> >>4) return to saved directory
[...]
> 
>     REM place a 'cd ' in the file old_dir.bat
>     copy \batch\cd.txt \batch\old_dir.bat
>     REM append the current directory path to the same file
>     cd >> \batch\old_dir.bat
>     REM now you can change directories at will and execute programs, etc.
>     cd \progs\etc
>     foo.exe
>     REM to return to the original directory, just execute the batch file
>     REM you created above:
>     \batch\old_dir.bat
>     REM and you are back at the original directory.
> 
Yes this will work fine AS LONG AS you execute \batch\old_dir.bat last
or as 'command /c \batch\old_dir.bat'... batch files exit otherwise and
and following statements are skipped.

Dave

_____________________

David P. Chassin
Rensselaer Polytechnic Institute	              |
School of Architecture			            __+__
Troy, NY   12181			           /  _  \
USA						   | | | |
					  /=======/   =   \=======\
(518) 266-6461				  |   _   |   _   |   _   |
					  |  | |  |  | |  |  | |  |
chassin@csv.rpi.edu			  |   =   |  | |  |   =   |
=======================================================================

phil@sci.UUCP (03/19/87)

In article <839@bucsb.bu.edu.UUCP>, keast@bucsb.bu.edu.UUCP (Kenneth East) writes:
> >>I am trying to use a batch file to:
> >>
> >>1) save the current directory
> >>2) goto another directory
> >>3) execute some program
> >>4) return to saved directory
> [...]
> >>Anybody out there have any idea how I can do this?  Step one is the problem
> Actually there is a very simple way to do it with I/O re-direction:
> 
> 1)  Create a file called cd.txt which contains ONLY the two letters
>     'cd' followed by a space followed by a ^Z (end of file).  Let's say
>     that we have created such a file and placed it in the
>     directory \batch.
> 
> 2)  You will have to place the following in a batch file which
>     in turn will execute your program(s).  For example:
> 
>     REM place a 'cd ' in the file old_dir.bat
>     copy \batch\cd.txt \batch\old_dir.bat
>     REM append the current directory path to the same file
>     cd >> \batch\old_dir.bat
>     REM now you can change directories at will and execute programs, etc.
>     cd \progs\etc
>     foo.exe
>     REM to return to the original directory, just execute the batch file
>     REM you created above:
>     \batch\old_dir.bat
>     REM and you are back at the original directory.
> 
> Please, no flames.  I know it's inelegant, but it works fine, and I have
> better things to do than writing a "real" program to do this.  -- 
> ======================================+=================================

The above approach works great but is not quite general enough. It does
not handle the case where the target directory is on a different drive
than the original drive. the simple fix is to add a couple more commands:
	after the first cd >> ... line that puts the old directory on the file
	add
		cd \ to get to the current drives root dir
		cd >> ... to put the drive name on the file
Now when the return file is executed you will not only cd to the right
directory but also change drive default back if it has been changed


the rest of this text is because
of the fact that
my stupid mailer
requires that there
be more new text
than included text

how is that for fine
programming?
alas, how often does one get to
have a program
that enforces efficiency
by insisting on more
text than you need.

anyone have a pointer to the
great programmer in the sky
who did us this favor?

li@tybalt.caltech.edu.UUCP (03/20/87)

In article <3381@iuvax.UUCP> bobmon@iuvax.UUCP (Che Flamingo) writes:
>x1.micky@cu20b.columbia.edu (Micky Liu) writes:
>>
>>I am trying to use a batch file to:
>>
>>1) save the current directory
>>2) goto another directory
>>3) execute some program
>>4) return to saved directory

There is a simple way to do something similar to this, although it really isn't
a solution, just a patch, but it doesn't take too much effort, and may be
adequate for what you want.

Use the SUBST command to assign a logical drive, say x:(assuming you have
included a line "lastdrive=z" in your config.sys) to the other directory you
want(or just the same drive and feed it the path).  Then change drives(just
"x:").  DOS remembers the current directory in each drive, so when you come
back c:(assuming you just work off c:, otherwise, this won't work), you will
still be in the old directory.

I have used the same trick to do a whereis on just a subdirectory tree.  The
public domain version of whereis we have searches the entire disk(if there is
another version that accepts a path, I'd like to hear about it.)  The batch
file looked something like the following:

if "%2" == "" goto usage
subst x: %2
x:
whereis %1
c:
subst x: /d
goto end
:usage
echo Usage: whis file path
:end


li@tybalt.caltech.edu (James Li)           __   __
(also li@citromeo.caltech.bitnet)            | |
"Official KANK symbol"         ------>       | |