|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub pdf文件_分类归档()
Dim pa$, reg As Object, d As Object, sr$, k, t, fol As Object
Dim f As Object, fd As Object, fso As Object
Set fol = CreateObject("Shell.Application").BrowseForFolder(0, "GetFolder", 0)
If Not fol Is Nothing Then pa = fol.Items.Item.Path Else MsgBox "请选目标文件夹": Exit Sub
If Right(pa, 1) <> "\" Then pa = pa & "\"
Set fso = CreateObject("Scripting.FileSystemObject")
Set d = CreateObject("Scripting.Dictionary")
Set reg = CreateObject("VBScript.Regexp")
reg.Pattern = "^((?:(?!(?:\d+)?\.pdf).)+)"
For Each f In fso.GetFolder(pa).Files
If f.Name Like "*.pdf" Then
If Not InStr(f.Name, "~$") Then
sr = reg.Execute(f.Name)(0).submatches(0)
d(sr) = d(sr) & "|" & pa & f.Name
End If
End If
Next
k = d.Keys: t = d.Items
For i = 0 To d.Count - 1
If Not fso.FolderExists(pa & k(i)) Then
fso.CreateFolder pa & k(i)
fn = Split(Mid(t(i), 2), "|")
For j = 0 To UBound(fn)
ff = Mid(fn(j), InStrRev(fn(j), "\") + 1)
If Not fso.FileExists(pa & k(i) & "\" & ff) Then
fso.MoveFile fn(j), pa & k(i) & "\" & ff
End If
Next
Else
fn = Split(Mid(t(i), 2), "|")
For j = 0 To UBound(fn)
ff = Mid(fn(j), InStrRev(fn(j), "\") + 1)
If Not fso.FileExists(pa & k(i) & "\" & ff) Then
fso.MoveFile fn(j), pa & k(i) & "\" & ff
End If
Next
End If
Next
MsgBox "ok!"
End Sub
|
|