执行for循环以调用Windows应用程序

时间:2015-03-24 15:46:40

标签: vbscript infinite-loop

我有一个调用应用程序的现有VBScript,如果某个用户已经登录,如果应用程序通过用户干预退出,则循环,否则运行资源管理器:

' Declare variables
dim fso
dim shell
dim oWshNet

set shell = CreateObject("WScript.Shell")

' get name of user logging in
set oWshNet = Wscript.CreateObject("Wscript.Network")
sUser = oWshNet.Username

' Run Aplication if user is user1

if oWshnet.Username = "user1" Then
  do
    shell.exec "C:\Program Files\Canon\MP Navigator EX 4.0\mpnex40.exe",0,true
  loop
Else
  shell.run "C:\windows\system32\explorer.exe"
end if

我已更改为新应用程序,现在发现它只会使用shell.exec运行应用程序。 shell.run导致找不到路径错误。在没有循环的情况下使用shell.exec通常不会出现问题,但由于我需要一个不断重新检查应用程序退出的循环,我似乎无法使用带有,0,true参数的shell.exec。

我有什么想法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

坚持使用Run方法。 Exec不支持其他参数,特别是不支持等待已执行的程序返回。

你找不到"路径的原因"错误是因为您的路径包含空格。您需要在路径周围放置双引号(在双引号字符串内),以便整个路径被识别为一个单个标记而不是多个标记C:\ProgramFiles\Canon\MP,aso。

嵌套双引号可以像这样添加(双引号加倍将它们转义为字符串):

shell.Run """C:\Program Files\Canon\MP Navigator EX 4.0\mpnex40.exe""", 0, True

或使用引用功能(这是一种更通用的方法):

Function qq(str) : qq = """" & str & """" : End Function
...
shell.Run qq("C:\Program Files\Canon\MP Navigator EX 4.0\mpnex40.exe"), 0, True