Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows batch file starting directory when 'run as admin'

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.

like image 929
Marc Avatar asked Mar 23 '09 09:03

Marc


People also ask

Why do batch files start with @echo off?

If used in a batch file, echo on and echo off don't affect the setting at the command prompt. To prevent echoing a particular command in a batch file, insert an @ sign in front of the command. To prevent echoing all commands in a batch file, include the echo off command at the beginning of the file.

How do I get a batch file to start automatically?

The easiest way to run a batch file on a system startup is to place it in the Windows “Startup” folder or drop there a shortcut. Programs placed in this folder are meant to run automatically whenever the computer boots up.


2 Answers

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 
like image 92
Martin Avatar answered Oct 06 '22 16:10

Martin


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.

like image 30
Benoit Avatar answered Oct 06 '22 15:10

Benoit