103

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
  • stackoverflow.com/q/47234901/340790 points out the general problem here, that there are lots of answers all saying to modify the command script, and no answers explaining how to get the actual shortcut to work properly and have the initial working directory where the shortcut actually says it to be. It's worth noting that this working directory problem in shortcuts applies to more than scripts. It apples to executables and DLLs invoked via rundll32 as well. See it applying to cmd at stackoverflow.com/q/18756671/340790 for example.
    – JdeBP
    Commented Mar 11, 2021 at 8:39

8 Answers 8

134

Try to access the batch files path like this:

echo %~dp0

For more information see the following quote from the command for /? that describes how the above command works:

You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line
4
  • 1
    @stucampbell try the command 'for /?' to get a detailed description of how this works (scroll down to page 4)
    – Martin
    Commented Feb 8, 2011 at 11:26
  • Donny V try pushd below
    – nwgat
    Commented Jun 15, 2015 at 3:23
  • @Donny V it does work on Windows 8.1 - I tried it. Keep in mind that this will only work if you are running it within a batch file.
    – Martin
    Commented Jun 16, 2015 at 4:32
  • Still works on Win10
    – Bill O
    Commented Feb 11, 2023 at 20:11
63

Better than cd is pushd which will

  • change drive letter if starting from D:\...
  • assign a drive letter if on a UNC network path

So pushd %~dp0 is good.

Good practice is then to call popd when done.

2
  • Great solution for systems with multiple volumes, and works in Windows 7, 8, 8.1, and 10, +1 Commented Aug 15, 2015 at 3:24
  • pushd "..." && ( ... & popd ) to chain it with popd on a single line (& needs for single line)
    – Andry
    Commented Nov 4, 2022 at 15:03
50

This should solve your problem by setting the working directory for the batch file back to the current directory:

Include these two lines at the top of your .bat script:

@setlocal enableextensions
@cd /d "%~dp0"

Found at: http://www.codeproject.com/Tips/119828/Running-a-bat-file-as-administrator-Correcting-cur

1
  • 3
    This is perfect. Solved my "file not found" issue when running the bat file as administrator.
    – Aloha
    Commented Jul 7, 2017 at 4:35
5

To fix this problem, include these two lines at the top of your .bat script:

@setlocal enableextensions
@cd /d "%~dp0"
1

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

2
  • 2
    That won't change drive letter. Commented May 2, 2014 at 16:59
  • This is actually the best solution, just change it to cd %0/../ 👍 Commented Jan 16, 2020 at 14:06
1

@setlocal enableextensions

@cd /d "%~dp0"

0
0

You can CD directly from the file name by adding the parent (not tested in windows 8.x, but has worked "forever" as far as I can remember).

CD %FILENAME%\..

and CD will change drives as well using /D, which is shown above but not explicitly mentioned so might be missed. CD /D %FILENAME%\..

(FOR /? IF /? SET /? CALL /? GOTO /? all provide highly useful reading if you use cmd.exe, I reread them once in a while.)

-1

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%

1
  • 4
    You can use %~dp0 directly. No need to invoke for here.
    – Joey
    Commented Oct 27, 2010 at 11:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.