5

Take an excruciatingly simple batch file:

echo hi
pause

Save that as test.bat. Now, make a shortcut to test.bat. The shortcut runs the batch file, which prints "hi" and then waits for a keypress as expected. Now, add some argument to the target of the shortcut. Now you have a shortcut to:

%path%\test.bat some args

The shortcut runs the batch file as before.

Now, run the shortcut as administrator. (This is on Windows 7 by the way.) You can use either right-click -> Run as Administrator, or go to the shortcut's properties and check the box in the advanced section. Tell UAC that it's okay and once again the shortcut runs the batch file as expected.

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

%path%\test.bat "some args"

Now try the shortcut as administrator. It doesn't work this time! A command window pops up and and disappears too fast to see any error. I tried adding > test.log 2>&1 to the shortcut, but no log is created in this case.

Try running the same shortcut (with the double quotes) but not as Administrator. It runs the batch file fine. So, it seems the behavior is not because of the double quoted parameters, and it's not because it's run as Administrator. It's some weird combination of the two.

I also tried running the same command from an administrator's command window. This ran the batch file as expected without error. Running the shortcut from the command window spawned a new command window which flashed and went away. So apparently the issue is caused by a combination of administrator, the shortcut, and the double quotes.

I'm totally stumped, does anyone have any idea what's going on? I'd also like a workaround.

4

6 回答 6

6

我刚刚对此运行了进程监视器,这就是我所看到的:

以用户身份运行:

cmd /c ""C:\Users\Sunbelt\Desktop\test.bat" "一些参数""

以管理员身份运行:

"C:\Windows\System32\cmd.exe" /C "C:\Users\Sunbelt\Desktop\test.bat" "一些参数"

出于某种原因,以管理员身份运行的案例没有引用整个命令。它似乎正在尝试运行命令:

C:\Users\Sunbelt\Desktop\test.bat" "一些参数

我猜想由于第一个空格被引用,它实际上试图运行以下命令:

“C:\Users\Sunbelt\Desktop\test.bat some”参数

在进程监视器日志中,“C:\Users\Sunbelt\Desktop\test.bat some”的文件系统条目为“NO SUCH FILE”。我不知道为什么以管理员身份运行时会有所不同,但这似乎正在发生。

于 2010-03-23T14:40:55.147 回答
3

要解决此问题,请在不带空格的路径上创建另一个 bat 文件,并且文件名不带空格,只需执行以下操作:

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

此辅助 bat 文件可以以管理员身份运行。

您现在可以创建此辅助 bat 文件的快捷方式,并在快捷方式的高级选项中选中以管理员身份运行。快捷方式可以放置在带有空格的路径上,并且可以有一个包含空格的文件名。

于 2011-12-28T00:08:43.983 回答
0

在我的情况下,我只想传递一个文件名作为参数,但路径有空格。

我找到了一个适用于这种情况的解决方案(如果可以截断文件名)。

使用 CALL.exe 的语法创建另一个 bat 文件 (input_gate.bat) 以删除路径中的空格。假设快捷方式名为 test.lnk 并且与 input_gate.bat 位于同一路径上:

call %~sdp0test.lnk %~sf1

这个作为参数传递给 test.bat 的完整文件名的短格式,具有管理员权限。

  • %~sdp0 -> 是短格式的当前路径(对于 input_gate.bat)。
  • %~sf1​​ -> 是传递给 input_gate.bat 的第一个参数(在我的例子中是带空格的完整文件名)
于 2013-07-14T06:48:43.683 回答
0

这在 Windows 7 中对我有用:

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

尚未以管理员身份尝试过。如果 myscript.bat 包含空格,我认为它不会起作用

于 2016-02-26T22:17:24.400 回答
0

我终于想通了,以一种允许使用长文件名的方式(短文件名不适合我的用例)。我的解决方案适用于 Win 7、8 和 10。我认为它的灵感来自 Luke 提到缺少的双引号。

这里是:

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.) 将文件拖到快捷方式上。成功!(我希望...)

于 2017-10-18T01:18:48.047 回答
0

这里的答案对我有用:https ://superuser.com/questions/931003/passing-variables-with-space-to-the-command-line-as-admin

哪个是...

在目标cmd /C之前添加。我还必须确保我的脚本名称和路径没有空格,并在target中引用脚本的路径。not

于 2018-01-30T12:50:53.287 回答