Administrator crashes for batch file with double quotes

Take the painfully simple batch file:

echo hi
pause

Save this as test.bat. Now make a shortcut to test.bat. A command file is launched in the shortcut, which prints "hello" and then waits for the key to be pressed, as expected. Now add some argument to the target of the shortcut. You now have a shortcut for:

%path%\test.bat some args

The shortcut launches the batch file as before.

Now run the shortcut as administrator. (By the way, on Windows 7). You can use either right-click → "Run as administrator" or go to the properties of the shortcut and select the checkbox in the advanced section. Tell UAC that everything is in order, and once again the shortcut launches the batch file as expected.

Now change the arguments in the target shortcut to add double quotes:

%path%\test.bat "some args"

. ! , - . > test.log 2 > & 1 , .

( ), . . , , - , - , . - .

. , , . , . -, , .

, - , ? .

+5
6

Process Monitor, :

:

cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "some args""

:

"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "some args"

- " " . , :

C:\Users\Sunbelt\Desktop\test.bat" "some args

, , , :

"C:\Users\Sunbelt\Desktop\test.bat some" args

Process Monitor "NO SUCH FILE" "C:\Users\Sunbelt\Desktop\test.bat some". , , , , , .

+5

, bat , :

call "Long path to original bat file\Original bat file.bat"

bat admin.

bat . , .

+3

, .

, ( , ).

bat (input_gate.bat), , CALL.exe. , test.lnk , input_gate.bat:

call %~sdp0test.lnk %~sf1

test.bat .

  • % ~ sdp0 → ( input_gate.bat) .
  • % ~ sf1 → , input_gate.bat( )
0

Windows 7:

ShortcutTarget: C:\Windows\System32\cmd.exe /C myscript.bat Param1 "Param Two with spaces"
StartIn:        "C:\Path containing\my script"

Admin. , , myscript.bat

0

, , , ( ). Win 7, 8 10. , .

:

1.) , , , :

cmd /s /c ""path_to_batch_file"

, 2 1 , 2. , !

2.) :

set user_file=%1"


! , :

1.) "test.bat" :

@echo off
set user_file=%1"
echo The file is: %user_file%
pause

3.) . :

cmd /s /c ""%userprofile%\desktop\test.bat"

4.) . ! (...)

0

: https://superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin

...

Adding cmd /Cto the target . I also had to make sure that the name and path of the script do not have spaces, but notspecify the script path in target .

0
source

All Articles