The start directory of the Windows batch file, when it is run as an administrator,

I have a batch file that is in a directory and needs to be launched from there, because it updates the files in this directory.
This works just fine unless the user runs the batch file as an administrator (required for Vista). Then the start directory is C: \ Windows \ System32.

Is there any other way to find out from which directory the batch file was launched?
I do not want the user to enter the directory manually.

+56
windows directory windows-vista batch-file uac
Mar 23 '09 at 9:35
source share
6 answers

Try opening the path to the batch files as follows:

echo %~dp0 

See the following quote from the for /? Command for /? more information. which describes how this 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
+74
Mar 23 '09 at 9:42
source share

Better than cd is pushd which will

  • change the drive letter starting with D:\...
  • designate a drive letter if on a UNC network path

So pushd %~dp0 is good.

It is good practice to call popd upon completion.

+28
Feb 08 '11 at 12:59
source share

This should solve your problem by setting the working directory for the batch file 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

+18
May 15 '15 at 10:13
source share

I use:

cd% 0 ..

at the beginning of the batch file to change the directory to the directory into which the batch file was run.

-Mathew

0
Jun 01 '09 at 18:55
source share

You can burn a CD directly from the file name by adding a parent element (it was not tested in Windows 8.x, but it worked "forever", as far as I remember).

 CD %FILENAME%\.. 

and CDs will also modify the drives using / D, as shown above, but not explicitly stated, so it may be skipped. CD / D% FILENAME% \ ..

(FOR /? IF /? ASK /? CALL /? GO TO /? All this is very useful, if you use cmd.exe, I re-read them from time to time.)

0
Nov 20 '15 at 18:46
source share

The working solution is 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 the% BATDIR% directory

-one
Oct 25 '10 at 12:29
source share



All Articles