# The .bat file quandary...from hell!

**URL:** <https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993>\
**Category:** Factual Questions\
**Created:** [September 3, 2004, 6:15pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993 "2004-09-03T18:15:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 3, 2004, 6:15pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993/1 "2004-09-03T18:15:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dog80](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@Dog80](https://boards.straightdope.com/u/Dog80)\
**Post date:** [September 3, 2004, 6:22pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993/2 "2004-09-03T18:22:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dog80](https://avatars.discourse-cdn.com/v4/letter/d/6bbea6/32.png) [@Dog80](https://boards.straightdope.com/u/Dog80)\
**Post date:** [September 3, 2004, 6:25pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993/3 "2004-09-03T18:25:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FatBaldGuy](https://avatars.discourse-cdn.com/v4/letter/f/ecd19e/32.png) [@FatBaldGuy](https://boards.straightdope.com/u/FatBaldGuy)\
**Post date:** [September 3, 2004, 6:26pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993/4 "2004-09-03T18:26:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ximenean](https://avatars.discourse-cdn.com/v4/letter/x/aca169/32.png) [@Ximenean](https://boards.straightdope.com/u/Ximenean)\
**Post date:** [September 3, 2004, 6:28pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993/5 "2004-09-03T18:28:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 3, 2004, 6:37pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993/6 "2004-09-03T18:37:14Z")

</div>

> [@Fat Bald Guy](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Alan\_E\_Sheets](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@Alan\_E\_Sheets](https://boards.straightdope.com/u/Alan_E_Sheets)\
**Post date:** [September 3, 2004, 6:39pm UTC](https://boards.straightdope.com/t/the-bat-file-quandary-from-hell/262993/7 "2004-09-03T18:39:43Z")

</div>

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.
