4

So we just discovered this weird quirk. Easy enough to avoid but curious if any of the experts here know what's happening.

It seems that if you try to use the right-click "Run as Administrator" on a batch file that contains a space and an @ symbol in the path, it will fail to launch. It will run fine in a normal context, or in a command prompt that you previously elevated, but this specific set of circumstances causes it to unceremoniously fail.

I did some searching online and the only other reference I could find was a post where a user named CapnM had a similar problem and discovered the same issue - back in 2015

The right-click, Run As Administrator function in File Explorer fails if the path to the target contains both an at sign (@) and a space.

For example these paths work:

D:\foo\bar\foobar\  
D:\foo\bar\foo bar\  
D:\@foo\bar\foobar\   
D:\@foo\bar@\foobar\

But these prevent the target (batch file) from running:

D:\@foo\bar\foo bar\  
D:\foo\bar\@foo bar\

Is this just a straight up bug that hasn't been fixed for 4+ years?

Daniel B
  • 60,360
  • 9
  • 122
  • 163
therks
  • 234
  • 2
  • 10
  • 1
    This is probably older than 2015. Windows 7 SP1 also exhibits the same behavior and SP1 was released in 2010. – Anaksunaman Nov 19 '19 at 08:11

1 Answers1

4

The Windows code that malfunctions here probably dates from the early days of DOS. It is a real mess for handling special characters.

If you wish another example, create a file named D:\foo\file(.bat and try to run it using CMD, Admin permissions not required. Here is what happens:

enter image description here

In the above example, in the CMD command, the parenthesis delimits the command, so it’s as if you’re running the program C:\foo\file with (.bat as the first argument.

You can see from here that the ancient CMD code was written when file-names were much simpler and before non-alphanumeric characters were permitted.

Your problem with "Run as Administrator" probably runs into the same ancient code. The command that is used for runas is defined in the registry at the key HKEY_CLASSES_ROOT\batfile\shell\runas\command, and contains:

%SystemRoot%\System32\cmd.exe /C "%1" %*

Issuing "Run as Administrator" uses then CMD to run the command, and we know that it does not handle special characters too well. The code involved was never published by Microsoft, so we cannot comment on how badly it is written.

harrymc
  • 455,459
  • 31
  • 526
  • 924