5

Consider the following batch file test.bat:

CD C:\SOFTWARE
program.exe

This script is run from C:\ELSWHERE

C:\ELSWHERE> C:\test.bat

After the script exits the current working directory is now C:\SOFTWARE and not the previous working directory C:\ELSWHERE.

The script changes the working directory to C:\SOFTWARE because program.exe requires config.ini which is located in C:\SOFTWARE and locates it using the current working directory. If the software program.exe were to be run directly from, say, C:\ or C:\ELSWHERE using its absolute path name, the program will be unable to read its config and throw an error. The solution to this is to CD to the C:\SOFTWARE folder first and run the program from there.

The problem with doing this in a batch file is that this also changes the working directory of the previous environment, be it the host COMMAND.COM working directory or another batch file.

This is not a problem from an NT-based command prompt which has access to pushd and popd, along with other useful environment variables to preserve the old working directory. But is it possible to do this within the constraints of the old MS-DOS\Win9x command.com?

Zhro
  • 957

1 Answers1

2

This even works with MSDOS6.22

pwd.bat

@echo off

@echo @prompt @set drive=$N:$_ @set pwd=$P> temp.bat %comspec% /c temp.bat > temp2.bat call temp2.bat del temp.bat del temp2.bat echo %PWD%

It stores the current directory into the pwd variable and the current drive into the drive variable.

This can be used to restore the previous directory.

In your case

@echo off
call pwd.bat

CD C:\SOFTWARE program.exe

%drive% cd %pwd%

jeb
  • 433