如果 - Else If语句在VBScript中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果 - Else If语句在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语句有多好?请帮我。

如何改进此代码?

答案

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

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)

以上是关于如果 - Else If语句在VBScript中的主要内容,如果未能解决你的问题,请参考以下文章

VBA if...elseif...else语句

vbscript if else声明

Shell if else语句(详解版)

java if-else语句中的if条件后如果不加大括号能执行几句话?

vbscript 如果另一个返回变量不可为空,则VB.NET可空整数通过if语句可能没有任何结果= 0。

选择语句 if else