Back to home page

DOS ain't dead

Forum index page

Log in | Register

Back to the forum
Board view  Mix view

How to have MS-DOS "remember" logged drive and directory? (Users)

posted by bretjohn Homepage E-mail, Rio Rancho, NM, 12.03.2013, 17:03

> I want to make a batch file that "remembers" the current logged drive and
> directory and then later can "recall" and re-log me back onto that drive
> and directory after some operations have changed the logged drive and
> directory.

Here's the hopefully short and understandable explanation.

In DOS, changing drives and changing directories are actually two separate functions. This is actually a pretty critical concept. Look at the following sequence (this assumes a PROMPT $P$G has been issued):

S:\> C:
C:\> CD S:\UTIL
C:\> S:
S:\UTIL>

The CD command on the second line changed the working (logged?) directory on S:, but didn't actually "log" onto the S: drive -- it stayed on C:. Changing the working drive to S: is a different command. This is true both at the command line and when programs (like PUSHD and POPD) "ask" DOS to do it for them.

Now let's do a sequence using PUSHD and POPD, which will "remember" the current working drive and directory and allow you to restore it:

S:\> C:
C:\> PUSHD S:\UTIL
S:\UTIL> POPD
C:\> S:
S:\UTIL>

PUSHD saved the current working drive and directory, and then changed both the working drive and directory. POPD was then able to restore things back to the original working drive and directory. Unfortunately, this sequence also changed the working directory on S:, and never restored it back to where it was when it started.

The point of all this is to point out that you need to use PUSHD/POPD more than once if you want to really restore everything back to the way it was when you started. The goal of your batch files should be to restore _everything_ back to the way it was when you started. that is, not just the working drive and directory, but also the working directories on all drives that you may have accessed along the way.

If you try to do something like this with environment variables, you need at least two of them, not just one. The only time you could ever get by with just one is if you only had one drive (say, C:) and never used floppies or USB drives or CD-ROM's or RAM disks or network drives or ... In the unlikely event that you did only have one drive, a single environment variable wouldn't allow you to nest (CALL) batch files from each other.

You need to create a "stack" of saved drives and directories, and need to keep the drives and directories separate from each other. I set up most of my batch files similar to this:

@ECHO OFF
PUSHD C: <- Save current drive & directory, switch to C:
PUSHD \Path2Use <- Save current directory on C:, switch to directory I need
Program <- Run Program from C:\Path2Use
POPD <- Restore original working directory on C:
POPD <- Restore original working drive and directory

A few notes about the PUSHD/POPD in the link above. PUSHD creates files in the directory where the %TEMP% (or %TMP%) environment variable points. If %TEMP% is not pointed at a RAM drive (which gets erased automatically when you reboot), you can end up with a bunch of useless files created by PUSHD if you have problems with batch files or programs that cause reboots. If %TEMP% is not on a RAM drive, you may want to make sure to delete any files created by PUSHD from the previous session when you reboot (you should add something to your AUTOEXEC.BAT to handle this).

PUSHD/POPD also don't work natively with long file names, though there are ways to get around that if you really want to.

For many years I used some old PC Magazine utilities called PUSHDIR and POPDIR to do this. PUSHDIR is actually a TSR, so it stored things in memory instead of the hard drive. The disadvantage, of course, is that it used memory.

 

Complete thread:

Back to the forum
Board view  Mix view
22762 Postings in 2122 Threads, 402 registered users (1 online)
DOS ain't dead | Admin contact
RSS Feed
powered by my little forum