the directory your batch is being executed from

Convert your BATch files into EXEcutable format in one click.
Post Reply
amrs
Posts: 3
Joined: Sun May 21, 2006 1:10 am

Post by amrs »

Hello,

Was wondering if anyone was aware of a way of finding out which directory your batch file (wrapped in exe) is being executed from?

This is useful if you need to run an application that resides in the same directory as the exe file. Since the batch file is unwrapped to your temp directory before being executed, attempting to find the current working directory via batch commands won't get you the correct result.

Thanks.
daslick
Posts: 6
Joined: Sun May 21, 2006 4:55 am

Post by daslick »

You could use the following commands, depending on what you mean by find out.
cd>%tmp%/batchloc.txt (would send the location to the contents of the temporary folder/batchloc.txt)

or
set batchloc=%cd%
echo %batchloc%
amrs
Posts: 3
Joined: Sun May 21, 2006 1:10 am

Post by amrs »

Thanks daslik, that works. Seems like I was trying to overcomplicate things hence the problem I was getting.

My operating system is windows xp but I was wondering if you knew for sure whether that command would work on older versions of windows as well (e.g. windows 2000, etc)?

Some of my users have not updated their machines in ages!
daslick
Posts: 6
Joined: Sun May 21, 2006 4:55 am

Post by daslick »

It will work with 2000... Not so sure about 95/98... research it if you have machines with those.
No Problem :D
overkill
Posts: 5
Joined: Tue Apr 24, 2007 10:06 pm

Post by overkill »

I know this is dead, but for future readers:

relative paths will help you here...

If your batch file (compiled or not) is located in C:\dir1\dir2, you don't need to enter C:\dir1\dir2\file.exe to run file.exe from your batch script. just enter file.exe. as long as the file exists, it'll work. If file.exe is in dir1, and your batch in dir2, you can use ..\file.exe. If your batch is in dir1, and your exe is in dir2, you can use dir2\file.exe.

you can also use
IF EXIST file.exe (file.exe) ELSE (echo Error - file not found).

google relative paths for more info :D
Post Reply