如何在VBS中创建具有多个条件(不是:多个语句嵌套)的If语句?

时间:2014-01-14 13:00:55

标签: vba vbscript

在PHP中,如果我可以动态地将值与其他值列表进行比较:

$extension = "pdf";
if (!in_array($extension, array("docx", "xlsx", "txt")) {
  // do stuff to the pdf
}

我怎么能把它移植到VBS?由于我无法弄清楚如何在php中动态创建数组,我尝试了if语句有3个条件:

extension = objFso.GetExtensionName(objFile.Path)
If Not (extension = "docx") OR (extension = "xlsx") OR (extension = "txt") Then
  // do stuff to the pdf
End If

但这不起作用。条件被忽略了。两个问题:

  1. 为什么条件被忽略?
  2. 我怎样才能,而不是使用多个If条件或If语句,动态创建一个数组来进行比较?
  3. 非常感谢任何healp。

2 个答案:

答案 0 :(得分:4)

我知道的最短路径是(布尔类型

UBound(Filter(Array("docx", "xlsx", "txt"), "pdf")) > -1返回False

UBound(Filter(Array("docx", "xlsx", "txt"), "txt")) > -1返回True

你可以看到Array("doc", "xlsx", "txt")已经 on> ,我已经用原来的问题用两个不同的字符串替换了extension "pdf" "txt" extension = objFso.GetExtensionName(objFile.Path) ' extension is pdf ' the below produces false which in your logic means YES it's none of them If (UBound(Filter(Array("docx", "xlsx", "txt"), extension)) > -1) Then ' you assume it's pdf ' do stuff to the pdf End If 1}}和{{1}})

<小时/> 所以,例如

{{1}}

答案 1 :(得分:3)

select case

怎么样?
extension = lcase(objFso.GetExtensionName(objFile.Path))

select case extension
   case "docx", "xlsx", "txt"
     ' is one of the above
     ' do something
   case "zzz"
     ' its a .zzz
   case else
     ' its something else
end select

在你当前的逻辑对比中

If Not True Or True ...

If Not (True Or True) ...