The .bat file quandary...from hell!

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?

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

Oh, and use the full path, eg. cd > c:\windows\file.txt, or else the program will not find file.txt after changing directory.

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.

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 explains how to achieve the same result.

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

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.