“以管理员身份运行”时的 Windows 批处理文件起始目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/672693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch file starting directory when 'run as admin'
提问by Marc
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.
我有一个位于目录中的批处理文件,也必须从那里运行,因为它会更新此目录中的文件。
这工作得很好,除非用户以管理员身份运行批处理文件(Vista 上需要)。那么起始目录是C:\Windows\System32。
有没有办法仍然能够知道批处理文件是从哪个目录运行的?
我不希望用户手动输入目录。
回答by Martin
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:
有关更多信息,请参阅for /?
描述上述命令如何工作的命令中的以下引用:
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
回答by Benoit
Better than cd
is pushd
which will
优于cd
就是pushd
这将
- change drive letter if starting from
D:\...
- assign a drive letter if on a UNC network path
- 如果从更改驱动器号
D:\...
- 如果在 UNC 网络路径上,则分配驱动器号
So pushd %~dp0
is good.
所以pushd %~dp0
很好。
Good practice is then to call popd
when done.
好的做法是popd
在完成后调用。
回答by NMrt
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:
在 .bat 脚本的顶部包含这两行:
@setlocal enableextensions
@cd /d "%~dp0"
Found at: http://www.codeproject.com/Tips/119828/Running-a-bat-file-as-administrator-Correcting-cur
位于:http: //www.codeproject.com/Tips/119828/Running-a-bat-file-as-administrator-Correcting-cur
回答by Mathew
I use:
我用:
cd %0..
光盘 %0..
at the beginning of the batch file to change directory to the directory where the batch file was started in.
在批处理文件的开头将目录更改为批处理文件的启动目录。
-Mathew
-马修
回答by Malcolm
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(未在 Windows 8.x 中测试,但据我所知已经“永远”工作了)。
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%\..
并且 CD 也会使用 /D 更改驱动器,这在上面显示但未明确提及,因此可能会被遗漏。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.)
(对于 /? IF /? SET /? CALL /? GOTO /? 如果您使用 cmd.exe,所有这些都提供非常有用的阅读,我偶尔会重读一次。)
回答by Syed Abdul Haseeb
@setlocal enableextensions
@setlocal 启用扩展
@cd /d "%~dp0"
@cd /d "%~dp0"
回答by domotor
A working solution here:
一个有效的解决方案:
http://www.vistax64.com/vista-general/79849-run-administrator-changes-default-directory.html
http://www.vistax64.com/vista-general/79849-run-administrator-changes-default-directory.html
FOR /F %%I IN ("%0") DO SET BATDIR=%%~dpI
FOR /F %%I IN ("%0") DO SET BATDIR=%%~dpI
ECHO The batch file is located in directory %BATDIR%
ECHO 批处理文件位于目录 %BATDIR%