为什么“以管理员身份运行"更改(有时)批处理文件的当前目录? [英] Why does 'Run as administrator' change (sometimes) batch file's current directory?

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

问题描述

我有一个批处理文件,该文件与我要xcopy的文件位于同一目录中.但是由于某种原因,找不到该文件.

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.

我认为当前目录始终是批处理文件所在的位置.

I thought that current directory was always where the batch file was located.

我以管理员身份运行批处理文件.这是在Windows 7 64位台式计算机上发生的.

I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.

批处理文件:

@ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE

错误:

File not found - File1.txt
0 File(s) copied

推荐答案

哪个目录是使用上下文菜单项以管理员身份运行启动批处理文件时的当前工作目录,取决于用户帐户控制(UAC)设置为当前用户.

Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.

这可以通过以下小的批处理文件C:\Temp\Test.bat进行演示:

This can be demonstrated with following small batch file C:\Temp\Test.bat:

@echo Current directory is: %CD%
@pause


用户帐户控制设置中选择了


With having selected in User Account Control Settings

默认-仅在程序尝试对计算机进行更改时通知我

  • 当我更改Windows设置时不会通知我

并使用以管理员身份运行,Windows使用注册表项

and using Run as administrator, Windows uses registry key

HKEY_CLASSES_ROOT\batfile\shell\runasuser\command

此注册表项不包含用于执行批处理文件的默认字符串.而是使用具有CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}的字符串值DelegateExecute.

This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.

结果是打开一个对话框窗口,其标题为用户帐户控制和文本:

The result is opening a dialog window with title User Account Control and text:

是否要允许以下程序对此计算机进行更改?

Do you want to allow the following program to make changes to this computer?

程序名称:Windows命令处理器
经过验证的发布者:Microsoft Windows

Program name: Windows Command Processor
Verified publisher: Microsoft Windows

在用户确认后,Windows会临时打开一个新的用户会话,就像在命令行

After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.

在这个新的用户会话中,当前执行Windows注册表中定义的默认字符串键的命令时,当前工作目录为%SystemRoot%\System32

In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key

HKEY_CLASSES_ROOT\batfile\shell\runas\command

这是:

%SystemRoot%\System32\cmd.exe /C "%1" %*

因此将打开一个控制台窗口,其标题为 C:\ Windows \ System32 \ cmd.exe 和2行:

Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:

Current directory is: C:\Windows\System32
Press any key to continue . . .

按任意键后,批处理完成,这将导致关闭cmd.exe,这将导致关闭用户会话.

After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.

但是在用户帐户控制设置

从不通知我

  • 程序尝试安装软件或对我的计算机进行更改

  • Programs try to install software or make changes to my computer

我对Windows设置进行了更改

I make changes to Windows settings

行为不同,因为用户已经具有特权.

the behavior is different as the user has already elevated privileges.

现在Windows直接使用命令

Now Windows uses directly the command

%SystemRoot%\System32\cmd.exe /C "%1" %*

根据默认的密钥字符串

HKEY_CLASSES_ROOT\batfile\shell\runas\command

在当前用户会话中.

结果是打开一个标题为 C:\ Windows \ System32 \ cmd.exe 的控制台窗口,但在窗口中显示的是:

The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:

Current directory is: C:\Temp
Press any key to continue . . .

使用父进程的当前工作目录(Windows资源管理器作为桌面)来执行批处理文件,因为在这种情况下无需切换到其他用户会话.

The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.

PA 在他的答案中已经发布了2种可能的解决方案,在此我进行了细微的改进(pushd目录中用双引号引起来),并添加第三个.

PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.

  1. 使用 pushd popd 将当前目录更改为批处理文件的目录:

  1. Change current directory to directory of batch file using pushd and popd:

pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd

这也适用于UNC路径.在命令提示符窗口pushd /?中运行,以解释为什么它也适用于UNC路径.

This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.

在源和目标规范中使用批处理文件的目录:

Use directory of batch file in source and destination specifications:

%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y

  • 使用 cd 将工作目录更改为批处理文件的目录:

  • Change working directory to directory of batch file using cd:

    cd /D "%~dp0"
    %SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
    

    这不适用于UNC路径,因为默认情况下命令解释器 cmd 不支持将UNC路径作为当前目录,例如,请参见

    This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.

    这篇关于为什么“以管理员身份运行"更改(有时)批处理文件的当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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