简体   繁体   English

批处理脚本-是否与管理员不同

[英]Batch Script - different when admin or not

I've been trying to copy a file using a script, and I had the following code. 我一直在尝试使用脚本复制文件,并且有以下代码。 It displays as expected when run: C:\\Users\\Nico\\Desktop\\Stopthe.scr 运行时它将按预期显示:C:\\ Users \\ Nico \\ Desktop \\ Stopthe.scr

If I run it as Administrator, which it needs to work correctly, it displays a different path: C:\\Windows\\System32\\Stopthe.scr 如果我以管理员身份运行它(它需要正常工作),它将显示不同的路径:C:\\ Windows \\ System32 \\ Stopthe.scr

The script is I'm testing is on the desktop. 我正在测试的脚本是在桌面上。

:: don't output the commands that are running to screen
echo OFF
:: label for a goto statement - although not required atm
:start
:: clears the screen
cls
:: variable - path to where script is located
set herePath=%cd%\Stopthe.scr
:: %cd% returns the current directory of where the script is.

echo  %herePath%
echo.

pause

I want to locate the path, and it returns the correct path when running normally, but when ran as administrator, it doesn't return the path to the script (but presumably where the cmd.exe is located). 我想找到路径,并且在正常运行时它会返回正确的路径,但是当以管理员身份运行时,它不会将路径返回到脚本(但可能是cmd.exe所在的位置)。

I understood %cd% to return the current directory the script is in, which is what I want. 我理解%cd%返回脚本所在的当前目录,这就是我想要的。

Running a batch script as administrator always defaults the working directory (ie %CD% ) to C:\\Windows\\System32 . 以管理员身份运行批处理脚本始终会将工作目录(即%CD% )默认为C:\\Windows\\System32

The path to where script is located is (including trailing backslash) %~dp0 , so you could use 脚本所在的路径是(包括反斜杠) %~dp0 ,因此可以使用

set "herePath=%~dp0Stopthe.scr"

Resource: Command Line arguments (Parameters) , part Links relative to the Batch Script 资源: 命令行参数(参数)相对于批处理脚本的部件链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM