Whenever you try to execute a batch file whose name contains an ampersand or caret via right click and "run as administrator" - it will fail to run.
The reason seems to be a bug in the elevation routine of Windows. The batch file cannot be found because the cmd is obviously called with the file path as an unquoted argument.
Even if jeb mentioned this behavior could be avoided by changing the command in the related registry key my experiences are that it doesn't help either ...
*******************************
I found that odd behavior when I tried to save the relative path of a file into a variable (%~pnx0) but it seems to be a general problem. For that reason I simplified my code for testing ...
The name of my test file has a space (to protect the %0 from being unquoted) and an ampersand (that triggers the behavior I mentioned).
"test (&).bat"
Code: Select all
@prompt $g
rem Hello from %0
pause>nul
When running it unelevated via double click it displays
>rem Hello from "G:\Documents\test (&).bat"
>pause1>nul
As soon as you right-click and run as administrator it dies with an error message I wasn't able to read (the window closes too fast).
Even more strange:
I created a 2nd batch file in the same directory.
"call_test.bat"
Code: Select all
@prompt $g
call "%~dp0test (&).bat"
If I run this batch code elevated I don't have any problems to access %0 (or modified parameters derived from it).
>call "G:\Documents\test (&).bat"
>rem Hello from "G:\Documents\test (&).bat"
>pause1>nul
It seems I have to process the CMDCMDLINE variable to extract the name. However I'm wondering if somebody out there can confirm that behavior or is it just me (Win7 x86)?
Regards
aGerman