“以管理员身份运行”时 Windows 批处理文件启动目录

我有一个批处理文件,它在一个目录中,必须从那里运行,因为它更新这个目录中的文件。
除了当用户以管理员身份运行批处理文件(Vista 需要)之外,这种方法完全可以正常工作。然后起始目录是 C: WindowsSystem32。< br/> < br/> 还有什么办法可以知道批处理文件是从哪个目录运行的吗?
我不希望用户手动输入目录。

69942 次浏览

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

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

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%

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.

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

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.)

@setlocal enableextensions

@cd /d "%~dp0"

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

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