PDA

View Full Version : The .bat file quandary...from hell!


ultrafilter
09-03-2004, 01:15 PM
Ugh. I'm pressing the limits of the MS-DOS command system here, I think.

I need to make a .bat file that stores the current directory, switches to another directory based on what flavor of Windows we're on (NT-based or 9x), runs an executable, and changes back to the original directory.

So far, I know how to run the executable. I'm pretty well stuck with the .bat format due to various issues. Any help?

Dog80
09-03-2004, 01:22 PM
The command cd will give you the current directory. cd > file.txt will save the current directory to the file.txt

I don't remember now how you can feed the contents of file.txt to the cd command. I tried cd < file.txt, but it didn't work

Dog80
09-03-2004, 01:25 PM
Oh, and use the full path, eg. cd > c:\windows\file.txt, or else the program will not find file.txt after changing directory.

FatBaldGuy
09-03-2004, 01:26 PM
Do you really need to change the directory? Unless the program you're running needs some external files in the same directory you can either provide a fully qualified path to the .exe or make sure it's in your Path environment variable. Then you can run the program and still be in the directory you started from.

Ximenean
09-03-2004, 01:28 PM
On Win2K (and XP?) you can use the built-in variable CD, e.g. set olddir=%CD%, followed by cd %olddir% later. On Win9x, this page (http://www.fpschultze.de/bsh.htm#a4) explains how to achieve the same result.

ultrafilter
09-03-2004, 01:37 PM
Do you really need to change the directory? Unless the program you're running needs some external files in the same directory you can either provide a fully qualified path to the .exe or make sure it's in your Path environment variable. Then you can run the program and still be in the directory you started from.

The fully-qualified path is going to depend on the OS, which presents an issue.

Alan E Sheets
09-03-2004, 01:39 PM
My two approaches would be:

1) IF %VER%=="Windows_NT" goto changedir1
GOTO CHANGEDIR2
:CHANGEDIR1
CD whatever1
GOTO FINISH
:CHANGEDIR2
CD whatever2
:FINISH


2) IF EXIST C:\WINDOWS\NUL CD whatever1
IF EXIST C:\WINNT\NUL CD whatever2

Either way, horrible spaghetti logic.