Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
625 views
in Technique[技术] by (71.8m points)

process - Why does 'Run as administrator' change (sometimes) batch file's current directory?

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.

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

Batch file:

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

Error:

File not found - File1.txt
0 File(s) copied
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

@echo Current directory is: %CD%
@pause

With having selected in User Account Control Settings

Default - Notify me only when programs try to make changes to my computer

  • Don't notify me when I make changes to Windows settings

and using Run as administrator, Windows uses registry key

HKEY_CLASSES_ROOTatfileshell
unasusercommand

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?

Program name: Windows Command Processor
Verified publisher: Microsoft Windows

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

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

which is:

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

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

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


But with having selected in User Account Control Settings

Never notify me when

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

  • I make changes to Windows settings

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

Now Windows uses directly the command

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

according to default string of key

HKEY_CLASSES_ROOTatfileshell
unascommand

in current user session.

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

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 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. Change current directory to directory of batch file using pushd and popd:

    pushd "%~dp0"
    %SystemRoot%System32xcopy.exe "File1.txt" "File2.txt" /Y
    popd
    

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

  2. Use directory of batch file in source and destination specifications:

    %SystemRoot%System32xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
    
  3. Change working directory to directory of batch file using cd:

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

    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...