如果 - Else If语句在VBScript中

时间:2017-12-21 10:25:46

标签: vbscript

Else If exist "K:\ICT  project"
    (Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run Chr(34) & "K:\ICT  project" & Chr(34), 0
    Set WshShell = Nothing)
Else If exist "F:\ICT  project"
    (Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run Chr(34) & "F:\ICT  project" & Chr(34), 0
    Set WshShell = Nothing)
Else If exist "E:\ICT  project"
    (Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run Chr(34) & "E:\ICT  project" & Chr(34), 0
    Set WshShell = Nothing)
Else If exist "D:\ICT  project"
    (Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run Chr(34) & "D:\ICT  project" & Chr(34), 0
    Set WshShell = Nothing)
Else If exist "C:\ICT  project"
    (Set WshShell = CreateObject("WScript.Shell")
    WshShell.Run Chr(34) & "C:\ICT  project" & Chr(34), 0
    Set WshShell = Nothing)

代码有什么问题?以及If语句的使用情况如何?请帮帮我。

如何改进此代码?

1 个答案:

答案 0 :(得分:0)

要搜索不同驱动器上的程序并调用它(可能带有参数和正确的引号),请在驱动器上使用循环:

Option Explicit

Const csPrg = "pipapo"

Function qq(s) : qq = """" & s & """" : End Function

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim sDL, sFSpec, sCmd
For Each sDL In Split("A:\ C:\ K:\ E:\")
    sFSpec = goFS.BuildPath(sDL, csPrg)
    sCmd = Join(Array("%comspec% /c", qq(sFSpec), "/p:arm", qq("one two three"), 4711))
    WScript.Echo sCmd 
    If goFS.FileExists(sFSpec) Then Wscript.Echo "  ==> now run sCmd (and perhaps Exit For)"
Next

输出:

cscript 47922850.vbs
%comspec% /c "A:\pipapo" /p:arm "one two three" 4711
%comspec% /c "C:\pipapo" /p:arm "one two three" 4711
%comspec% /c "K:\pipapo" /p:arm "one two three" 4711
%comspec% /c "E:\pipapo" /p:arm "one two three" 4711
  ==> now run sCmd (and perhaps Exit For)