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

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

问题描述

我有一个批处理文件,它与我要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:TempTest.bat来演示:

This can be demonstrated with following small batch file C:TempTest.bat:

@echo Current directory is: %CD%
@pause

<小时>

用户帐户控制设置

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

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

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

and using Run as administrator, Windows uses registry key

HKEY_CLASSES_ROOTatfileshell
unasusercommand

此注册表项不包含用于执行批处理文件的默认字符串.取而代之的是带有 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 会临时打开一个新的用户会话,就像在命令行上使用时一样 RunAs.

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

在这个新的用户会话中,当前的工作目录是 %SystemRoot%System32 现在执行在 Windows 注册表中定义的命令,使用默认的密钥字符串

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_ROOTatfileshell
unascommand

即:

%SystemRoot%System32cmd.exe /C "%1" %*

因此会打开一个控制台窗口,标题为 C:WindowsSystem32cmd.exe 和两行:

Therefore a console window is opened with title C:WindowsSystem32cmd.exe and the 2 lines:

Current directory is: C:WindowsSystem32
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%System32cmd.exe /C "%1" %*

根据key的默认字符串

according to default string of key

HKEY_CLASSES_ROOTatfileshell
unascommand

在当前用户会话中.

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

The result is opening a console window also with title C:WindowsSystem32cmd.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. 使用pushdpopd将当前目录更改为批处理文件的目录:

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

pushd "%~dp0"
%SystemRoot%System32xcopy.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%System32xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y

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

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

    cd /D "%~dp0"
    %SystemRoot%System32xcopy.exe "File1.txt" "File2.txt" /Y
    

    这不适用于 UNC 路径,因为默认情况下命令解释器 cmd 不支持将 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天全站免登陆