简体   繁体   中英

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

If I run it as Administrator, which it needs to work correctly, it displays a different path: 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).

I understood %cd% to return the current directory the script is in, which is what I want.

Running a batch script as administrator always defaults the working directory (ie %CD% ) to C:\\Windows\\System32 .

The path to where script is located is (including trailing backslash) %~dp0 , so you could use

set "herePath=%~dp0Stopthe.scr"

Resource: Command Line arguments (Parameters) , part Links relative to the Batch Script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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