VBA RegEx查找文件
我需要find文件夹中的文件,我有3个文件命名的情况下:
- DI0425522.pdf
- AL-DN-DI0425523.pdf
- AL-DN-DI0425524-2016-11-17_1108.pdf
我可以处理第一个和第二个案子,但是我也需要find第三个。 3.文件名的最后16个字符可以变化,所以我想用RegExp来匹配它,然后复制另一个文件夹中的所有文件。
该string存储在一个Excel单元格,但只有“DI #######”命名
- DI0425522(A2细胞)
- DI0425523(A3单元格)
- DI0425524(A4格)
这是代码,但它不起作用:它显示错误438“对象不支持此属性或方法”上If Dir(Source & "\DI\" & "AL-DN-" & ValoreCella & regex & ".Pdf") <> "" Then
行
Sub cerca() Dim T As Variant Dim D As Variant T = VBA.Format(VBA.Time, "hh.mm.ss") D = VBA.Format(VBA.Date, "yyyy.MM.dd") Dim Ricercatore As Variant Ricercatore = Cells(1, 3) Dim Source As String Dim Dest As String Source = "\\it-s-bolo02\WORKGROUP\Comune\000_0_______ COMUNE 2011\15_TECNICO\AAA - RICERCA DDT\ALSS\DDT" Dest = "\\it-s-bolo02\WORKGROUP\Comune\000_0_______ COMUNE 2011\15_TECNICO\AAA - RICERCA DDT\ALSS\Ricerca\Ricerca " & D & " " & T & " " & Ricercatore MkDir Dest Dim ValoreCella As Variant, r As Long, DDTmancanti As Variant r = 2 Do Until Cells(r, 1) = "" ValoreCella = Cells(r, 1) If Dir(Source & "\DI\" & ValoreCella & ".Pdf") <> "" Then FileCopy Source & "\DI\" & ValoreCella & ".Pdf", Dest & "\" & ValoreCella & ".Pdf" Else If Dir(Source & "\DI\" & "AL-DN-" & ValoreCella & ".Pdf") <> "" Then FileCopy Source & "\DI\" & "AL-DN-" & ValoreCella & ".Pdf", Dest & "\" & "AL-DN-" & ValoreCella & ".Pdf" Else Dim regex As Object, str As String Set regex = CreateObject("VBScript.RegExp") str = "-([0-9]*)-([0-9]*)-([0-9]*)_([0-9]*)" With regex .Pattern = str .Global = True End With If Dir(Source & "\DI\" & "AL-DN-" & ValoreCella & regex & ".Pdf") <> "" Then FileCopy Source & "\DI\" & "AL-DN-" & ValoreCella & regex & ".Pdf", Dest & "\" & "AL-DN-" & ValoreCella & regex & ".Pdf" Else If Dir(Source & "\Altro\" & ValoreCella & ".Pdf") <> "" Then FileCopy Source & "\Altro\" & ValoreCella & ".Pdf", Dest & "\" & ValoreCella & ".Pdf" Else DDTmancanti = DDTmancanti & ValoreCella & vbCrLf End If End If End If End If r = r + 1 Loop Dim FF As Long FF = FreeFile Open (Dest & "\" & "0 - DDT_mancanti.txt") For Output As #FF Write #FF, DDTmancanti Close #FF MsgBox "Operazione eseguita" Shell "explorer.exe " + Dest, vbNormalFocus End Sub
感谢帮助
RegExp
是一个对象,它没有默认的属性,所以你不能把它连接成一个string并像通配符一样使用它。 如果您需要使用Dir
查找匹配的文件,则需要遍历目录并使用正则expression式testing每个生成的文件名,直到find匹配项。 您可以通过在Dir
的Pathname
参数中使用通配符来减less一些无关的匹配 – 例如, Source & "\DI\*DI???????*.pdf"
应该会消除其中的大部分。
此外,因为不能在Dir
使用“部分”正则expression式,所以需要构build一个正则expression式, 完全匹配任何文件规范。 这应该基于您的示例文件名称工作:
^(AL-DN-)?DI\d{7}(-\d{4}-\d{2}-\d{2}_\d{4})?\.pdf$
这相当简化你的主循环。 添加是否find匹配的标志,并在find匹配时提前退出。 像这样的东西应该更接近你所需要的(未经testing):
'... r = 2 With New RegExp .Pattern = "^(AL-DN-)?DI\d{7}(-\d{4}-\d{2}-\d{2}_\d{4})?\.pdf$" Do Until Cells(r, 1) = "" Dim found As Boolean ValoreCella = Cells(r, 1) Dim current As String current = Dir$(Source & "\DI\*DI???????*.pdf") Do Until current = vbNullString If .Test(current) Then 'Found the file. FileCopy current, Dest & "\" & current found = True Exit Do End If current = Dir$() Loop If Not found Then DDTmancanti = DDTmancanti & ValoreCella & vbCrLf found = False r = r + 1 Loop End With Dim FF As Long '...
我试过,但不行。 在这里你的代码与评论:
With New RegExp .Pattern = "^(AL-DN-)?DI\d{7}(-\d{4}-\d{2}-\d{2}_\d{4})?\.pdf$" Do Until Cells(r, 1) = "" Dim found As Boolean ValoreCella = Cells(r, 1) Dim current As String current = Dir$(Source & "\DI\*DI???????*.pdf") Do Until current = vbNullString If .Test(current) Then 'Found the file. FileCopy current, Dest & "\" & current 'Error 53 File not found--> current var is the first file found without Source string, see image attached
found = True Exit Do End If current = Dir$() Loop If Not found Then DDTmancanti = DDTmancanti & ValoreCella & vbCrLf found = False r = r + 1 Loop End With Dim FF As Long
我试过这个MOD:
With New RegExp .Pattern = "^(AL-DN-)?DI\d{7}(-\d{4}-\d{2}-\d{2}_\d{4})?\.pdf$" Do Until Cells(r, 1) = "" Dim found As Boolean ValoreCella = Cells(r, 1) Dim current As String current = Dir$(Source & "\DI\*DI???????*.pdf") Do Until current = vbNullString If .Test(current) Then 'Found the file. Dim SourceDI, DestDI As String SourceDI = Source & "\DI\" & current DestDI = Dest & "\" & current FileCopy SourceDI, DestDI found = True Exit Do End If current = Dir$() Loop If Not found Then DDTmancanti = DDTmancanti & ValoreCella & vbCrLf found = False r = r + 1 Loop End With
文件string现在是正确的,但是没有ValoreCella值的testing,所以代码将返回在文件夹中find的第一个文件,然后停止
更新:
我以这种方式解决了没有RegExp的问题:
'... Do Until Cells(r, 1) = "" ValoreCella = Cells(r, 1) Dim current As String current = Dir$(Source & "\DI\*" & ValoreCella & "*.pdf") If current <> "" Then FileCopy Source & "\DI\" & current, Dest & "\" & current Else DDTmancanti = DDTmancanti & ValoreCella & vbCrLf End If r = r + 1 Loop '...
谢谢你的帮助
- 在Matlab中迭代逗号分隔的列表会给出错误:“FORexpression式逗号分隔的列表必须只有一个项目”。
- 从列中提取模式
- 使用VBA正则expression式来标识不包含特定单词的string
- VBA Excel正则expression式 – \ b字边界不匹配,如果字是在string的开始
- RegEx模式来标记除<img>之外的链接的空锚点
- CleanName不清洗“在Excel VBA中?
- 在Excel-VBA中使用RegExreplace文本
- 正则expression式在Excel中使用Visual Basic函数进行匹配和replace
- 使用Visual Basic for Applications的正则expression式错误