dynamic return to starting directory in batch file

G

Guest

In a batch program I'd like to dynamically save the starting drive and
directory before doing the work of the program, and then come back to it at
the end. I've been able get close redirecting the output of echo and the cd
commands to try to create another callable batch file, but haven't found a
combination that works. Has anyone done this successfully?
 
G

GTS

In a command prompt, type pushd /? and popd /? for help on these commands
which are designed for what you want to do.
 
Joined
Feb 3, 2006
Messages
147
Reaction score
1
=?Utf-8?B?UnVzcw==?= said:
In a batch program I'd like to dynamically save the starting drive and
directory before doing the work of the program, and then come back to it at
the end. I've been able get close redirecting the output of echo and the cd
commands to try to create another callable batch file, but haven't found a
combination that works. Has anyone done this successfully?

One way is to make a batch file that calls your file and sets up the drive etc using environment variables. For example

callmy.bat
SET here=%CD%
call my.bat here

my.bat
SET drive=%~d1
SET directory=%~p1
E:
CD \photos
%drive%
CD %directory%

This is an essentially trivial example and obviously in between the first two and last two lines you can insert whatever you want. Using the first parameter for here does not prevent you from using other parameters as well. If you run callmy within a command prompt you can see that it works.

The major problem is the fact that you have to use different commands to move the directory focus and the drive but this is provided for by the standard "hidden" environment variable %CD% and the standard processing supplied by DOS of batch file parameters.

EDIT GTS' solution is much neater
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top