views:

1196

answers:

3

I have a batch file which is in a directory and must be run from there as well because it updates files within this directory.
This works perfectly fine, except when the user runs the batch file as administrator (required on Vista). Then the starting directory is C:\Windows\System32.

Is there any way to still be able to know from which directory the batch file was run?
I dont want the user to enter the directory manually.

+1  A: 

Try to access the batch files path like this:

echo %~dp0
Martin
Fantastic, I didn't know this one.Thanks a lot
Marc
A: 

I use:

cd %0..

at the beginning of the batch file to change directory to the directory where the batch file was started in.

-Mathew

Mathew
This wont work because %0 contains the file name of the batch file not its directory.
Martin
A: 

A working solution here:

http://www.vistax64.com/vista-general/79849-run-administrator-changes-default-directory.html

FOR /F %%I IN ("%0") DO SET BATDIR=%%~dpI

ECHO The batch file is located in directory %BATDIR%

domotor
You can use `%~dp0` directly. No need to invoke `for` here.
Joey