问题描述
我有2个数组,一个带有文件路径,一个带有关键术语,如果它们与文件路径匹配,它们应该做一些事情。我无法匹配两个数组,我正在尝试使用通配符。
For d = 0 To C
If arr2(d) <> "" Then
For i = LBound(Arr3) To UBound(Arr3)
If (Arr3(i) <> "") And ("*" & Arr3(i) & "*") Like ("*" & arr2(d) & "*") Then
MsgBox arr2(d) & "----" & Arr3(i)
End If
Next
End If
Next
我已经移动了消息框来检查术语,然后再输入IF语句,这只会使我更加困惑。 这是3个数组项的msgBox文本,肯定会产生匹配吗?!
MBappform.txt ----应用程序
selfdec.txt ---- selfdec
confidentiality.txt ---- confi
I've tried this with and without double ASTERISK statements,I've tried one of each with ASTERISK statements.
I've got no idea why this isn't working.
任何帮助表示赞赏
编辑:
For d = 0 To C
If arr2(d) <> "" Then
For i = LBound(Arr3) To UBound(Arr3)
MsgBox "Do these Match? " & arr2(d) & " = " & Arr3(i)
& vbCrLf &
(Arr3(i) Like "*" & arr2(d) & "*")
If Arr3(i) Like "*" & arr2(d) & "*" Then
MsgBox "Works"
End If
Next
End If
Next
也许我提供了更多背景信息,因为这仍然无法正常工作。 arr2(D)是目录中文件名的数组,C是目录中文件数的计数。
对于arr2中的每个文件,我想交叉引用arr3中的一系列关键术语。
因此,对于每个文件,这应该交叉引用编号。 arr2的1和arr3的8,则应交叉引用arr2的2号和arr3的8。
无论出于何种原因,我列出的字符串似乎都无法提供匹配项,例如appform vs MBappform.txt无法提供匹配项。
我同意这些变量的名字很差,没有混淆。
伪代码就是这样...
圈出所有清单1 对于活动列表1元素-是否与任何列表2元素匹配
列表1.item1是否等于任何列表2 如果是,则保存列表2项以备后用 如果什么都不做,
将所有列表2项目与第一个列表1项目进行比较后,将下一个列表1项目(item2)与列表项目进行比较
...等等。
解决方法
测试就足够了
If Arr3(i) Like "*" & Arr2(d) & "*" Then
不需要测试Arr3(i) <> ""
,因为如果它为空,则无论如何都不匹配"*" & arr2(d) & "*"
。
仅在Like
运算符之后使用星号。
您可以轻松地用
进行测试MsgBox "MBappform.txt" Like "*" & "appform" & "*"
这将给您True
。
编辑:
看起来像是您混合了变量:
If Arr2(i) Like "*" & Arr3(d) & "*" Then
为变量使用有意义的名称。使用编号为Arr2
和Arr3
的变量名是最糟糕的事情。
编辑2:
请查看以下示例。通过此示例,您应该能够修复代码:
Option Explicit
Public Sub FindKeywordsInFileNames()
Dim ArrFileNames As Variant
ArrFileNames = Array("MBappform.txt","selfdec.txt","confidentiality.txt","SomeRandomFile.txt","AnotherFile.txt")
'instead of the above code retrieve your file names
Dim ArrKeyWords As Variant
ArrKeyWords = Array("appform","selfdec","confi")
'the keywords you are looking for in the filenames
Dim FileName As Variant
For Each FileName In ArrFileNames
Dim KeyWord As Variant
For Each KeyWord In ArrKeyWords
If FileName Like "*" & KeyWord & "*" Then
'key word is in the file name
Debug.Print FileName,KeyWord,"True"
Else
'key word is not in the file name
Debug.Print FileName,"False"
End If
Next KeyWord
Next FileName
End Sub
立即窗口中的输出应类似于:
MBappform.txt appform True
MBappform.txt selfdec False
MBappform.txt confi False
selfdec.txt appform False
selfdec.txt selfdec True
selfdec.txt confi False
confidentiality.txt appform False
confidentiality.txt selfdec False
confidentiality.txt confi True
SomeRandomFile.txt appform False
SomeRandomFile.txt selfdec False
SomeRandomFile.txt confi False
AnotherFile.txt appform False
AnotherFile.txt selfdec False
AnotherFile.txt confi False
编辑3
如果您没有文件名数组(如您在问题开始时所说的那样),那么您将需要另一个循环来与Dir()
一起使用:
Option Explicit
Public Sub FindKeywordsInFileNames()
Dim ArrKeyWords As Variant
ArrKeyWords = Array("appform","confi")
'the keywords you are looking for in the filenames
Dim FileName As String 'initialize folder (and first file name)
FileName = Dir("C:\Temp\")
Do While FileName <> vbNullString
Dim KeyWord As Variant
For Each KeyWord In ArrKeyWords
If FileName Like "*" & KeyWord & "*" Then
'key word is in the file name
Debug.Print FileName,"False"
End If
Next KeyWord
FileName = Dir() 'next filename
Loop
End Sub