“以管理员身份运行"时的 Windows 批处理文件起始目录 [英] Windows batch file starting directory when 'run as admin'

查看:30
本文介绍了“以管理员身份运行"时的 Windows 批处理文件起始目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于目录中的批处理文件,也必须从那里运行,因为它会更新此目录中的文件.
这工作得很好,除非用户以管理员身份运行批处理文件(Vista 上需要).那么起始目录是C:WindowsSystem32.

有什么办法还能知道批处理文件是从哪个目录运行的?
我不希望用户手动输入目录.

解决方案

尝试像这样访问批处理文件路径:

echo %~dp0

有关更多信息,请参阅命令 for/? 中的以下引用,该命令描述了上述命令的工作原理:

<前>您现在可以使用以下可选语法:%~I - 扩展 %I 删除任何周围的引号 (")%~fI - 将 %I 扩展为完全限定的路径名%~dI - 仅将 %I 扩展为驱动器号%~pI - 仅将 %I 扩展为路径%~nI - 仅将 %I 扩展为文件名%~xI - 仅将 %I 扩展为文件扩展名%~sI - 扩展路径只包含短名称%~aI - 将 %I 扩展为文件的文件属性%~tI - 将 %I 扩展到文件的日期/时间%~zI - 将 %I 扩展到文件大小%~$PATH:I - 搜索 PATH 中列出的目录环境变量并将 %I 扩展为找到的第一个的完全限定名称.如果环境变量名称不是已定义或未找到该文件搜索,然后此修饰符扩展为空字符串可以组合修饰符以获得复合结果:%~dpI - 仅将 %I 扩展为驱动器号和路径%~nxI - 仅将 %I 扩展为文件名和扩展名%~fsI - 将 %I 扩展为仅包含短名称的完整路径名%~dp$PATH:I - 搜索 PATH 中列出的目录%I 的环境变量并扩展为找到的第一个驱动器号和路径.%~ftzaI - 将 %I 扩展为类似 DIR 的输出行

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:WindowsSystem32.

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.

解决方案

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

这篇关于“以管理员身份运行"时的 Windows 批处理文件起始目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆