你怎么传递包含空格的vb​​s参数 [英] How do you pass parameters containing spaces in vbs

查看:196
本文介绍了你怎么传递包含空格的vb​​s参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本:

echo first arg is %1
pause

这是我想从一个VBScript调用像这样管理员权限:

That I want to invoke from a vbscript with admin rights like so:

Set UAC = CreateObject("Shell.Application")
UAC.ShellExecute "test.bat", "argument", "", "runas", 1

这工作正常,但我无法通过包含空格的一个参数path参数。基本上,我需要在封闭空间中的说法,但不管我试试,这是行不通的。它看起来像它会调用批处理,但是cmd窗口只是闪烁并消失,所以我不知道什么错误。
我试过:

This works ok but I am unable to pass a path argument that contains spaces as a single argument. Basically, I need to enclose the argument in spaces but whatever I try, it doesn't work. It looks like it invokes the batch, but the cmd window just flashes up and disappears so I don't know what is going wrong. I've tried:

UAC.ShellExecute "test.bat", """has spaces""", "", "runas", 1

UAC.ShellExecute "test.bat", Chr(34) & "has spaces" & Chr(34), "", "runas", 1

UAC.ShellExecute "test.bat", '"has spaces"', , "runas", 1

UAC.ShellExecute "cmd", "/c test.bat " & chr(34) & "has spaces" & chr(34), "", "runas", 1

但没有运气。任何建议我做错了吗?

But no luck. Any suggestions what I'm doing wrong?

推荐答案

要得到这个工作的方式是:

The way to get this working is:

UAC.ShellExecute "cmd", "/c """"c:\test.bat"" ""has spaces""""", "", "runas", 1

使用运行方式时要调用管理员用户权限要注意的重要一点是,工作目录将变更为C:\\ WINDOWS \\ SYSTEM32。因此,你需要指定到批处理文件的完整路径,以便它可以被发现。

The important thing to note when using runas to invoke admin user rights, is that the working directory will change to c:\windows\system32. Therefore, you need to specify the full path to the batch file so that it can be found.

我仍然不知道为什么路过的时候bat文件作为参数传递给CMD,而不是直接执行它这只是工作。

I'm still not sure why this only works when passing the bat file as an argument to "cmd", rather than executing it directly.

所有因亚历克斯ķ信贷因此,如果这帮助了你,给予好评他的评论。

All credit due to Alex K so if this helped you, upvote his comments.

在这种情况下,你需要引用整组参数,以CMD,以及各自的参数也。您还需要加倍的报价。它看起来有点疯狂,但是这是最好的解决办法,否则,如果您有任何参数的空间和bat文件路径将会失败。

In this case you need to quote the entire set of arguments to cmd, and each of the arguments also. You also need to double up the quotes. It looks a bit mad, but this is the best solution because otherwise it will fail if you have a space in any arguments and the bat file path.

这篇关于你怎么传递包含空格的vb​​s参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆