【发布时间】:2017-05-05 02:09:54
【问题描述】:
我对使用 VBA 很陌生,之前只做过基本的(基于数学的)编程。
关于任务: 我得到了一个包含大量文件的文件夹,我需要检查它们是否已经在系统中,这基本上需要检查按字母排序的文件夹,然后将这些文件夹放在按数字排序的文件夹中,最后包含在一个总体文件夹中。每个数字文件夹的字母文件夹数量会发生变化。我要返回的是丢失的文件、存在的文件以及它们的文件夹代码,例如 6B
关于查询: 我研究了数组、字典和集合,但我还没有得出关于什么是最好用的结论。我很烦恼用什么来抓取数据(文件名和文件夹代码),让比较和排序变得最简单
Sub comparison()
Dim AR, AQ, AF, AG, AH As Variant
stat_folder = "D:\Public_Digital_Files\Current folder\"
folder_address = "D:\Working_Files\Dan\searchfolder\"
AR = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R")
AQ = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q")
AF = Array("A", "B", "C", "D", "E", "F")
AG = Array("A", "B", "C", "D", "E", "F", "G")
AH = Array("A", "B", "C", "D", "E", "F", "G", "H")
Dim fso As New Scripting.FileSystemObject
'Dim files_current As New Collection
Dim files_current() As Variant
'Dim files_current As New Scripting.Dictionary
For Folder_num = 1 To 7
If Folder_num = 1 Or Folder_num = 4 Or Folder_num = 6 Then
For i = 0 To 6
stat_address = stat_folder & Folder_num & "\" & AG(i) & "\"
Dir stat_address
Fname = Dir(stat_address)
Do Until Fname = ""
x = x + 1
ReDim Preserve files_current(2, x)
files_current(0, x) = Fname
files_current(1, x) = Folder_num & AG(i)
'files_current.Add fname, folder_num & AG(i)
Fname = Dir
Loop
Next
ElseIf Folder_num = 2 Or Folder_num = 5 Then
For i = 0 To 5
stat_address = stat_folder & Folder_num & "\" & AF(i) & "\"
Dir stat_address
Fname = Dir(stat_address)
Do Until Fname = ""
x = x + 1
ReDim Preserve files_current(2, x)
files_current(0, x) = Fname
files_current(1, x) = Folder_num & AF(i)
'files_current.Add fname, folder_num & AF(i)
Fname = Dir
Loop
Next
ElseIf Folder_num = 3 Then
For i = 0 To 16
stat_address = stat_folder & Folder_num & "\" & AQ(i) & "\"
Dir stat_address
Fname = Dir(stat_address)
Do Until Fname = ""
x = x + 1
ReDim Preserve files_current(2, x)
files_current(0, x) = Fname
files_current(1, x) = Folder_num & AQ(i)
'files_current.Add fname, folder_num & AQ(i)
Fname = Dir
Loop
Next
ElseIf Folder_num = 7 Or Folder_num = 8 Then
For i = 0 To 17
stat_address = stat_folder & Folder_num & "\" & AR(i) & "\"
Dir stat_address
Fname = Dir(stat_address)
Do Until Fname = ""
x = x + 1
ReDim Preserve files_current(2, x)
files_current(0, x) = Fname
files_current(1, x) = Folder_num & AR(i)
'files_current.Add fname, folder_num & AR(i)
Fname = Dir
Loop
Next
ElseIf Folder_num = 9 Then
For i = 0 To 7
stat_address = stat_folder & Folder_num & "\" & AH(i) & "\"
Dir stat_address
Fname = Dir(stat_address)
Do Until Fname = ""
x = x + 1
ReDim Preserve files_current(2, x)
files_current(0, x) = Fname
files_current(1, x) = Folder_num & AH(i)
'files_current.Add fname, folder_num & AH(i)
Fname = Dir
Loop
Next
End If
Next
'lng = UBound(files_current)
'MsgBox "theres" & lng & "files"
Dim file_search() As Variant
'Dim file_search As New Collection
'Dim file_there As New Scripting.Dictionary
'Dim file_missing As New Collection
Dim file_there() As Variant
Dim file_missing() As Variant
Dir folder_address ' sets the folder as a directory
Fname = Dir(folder_address) ' assigns Fname as the file
Do Until Fname = "" ' loops it until there's no more files
c = c + 1 ' counter, used as an index for the length of files()
ReDim Preserve file_search(c)
If InStr(Fname, "_") = 0 Then
'file_search.Add Left(fname, InStr(fname, ".") - 1)
file_search(c) = Left(Fname, InStr(Fname, ".") - 1) ' assigns the cth element of files as the new file name
Else
'file_search.Add Left(fname, InStr(fname, "_") - 1)
file_search(c) = Left(Fname, InStr(Fname, "_") - 1)
End If
Fname = Dir ' assigns the new Fname
Loop
y = 1
L = 1
For j = 1 To c
'For Each Serch In file_search
b = 0
For k = 1 To 392
'For Each File In files_current.Keys
'Debug.Print File
'If InStr(File, Serch) = 1 Then
'Debug.Print File
If InStr(files_current(0, k), file_search(j)) = 1 Then
ReDim Preserve file_there(2, k)
file_there(0, y) = files_current(0, k)
'file_there.Add File, files_current(File)
file_there(1, y) = files_current(1, k)
b = 1
y = y + 1
End If
Next
If b = 0 Then
ReDim Preserve file_missing(L)
'file_missing.Add Serch
file_missing(L) = file_search(j)
L = L + 1
End If
Next
'a = 1
'b = 1
'For Each missing In file_missing
'Range("A" & a) = missing
'a = a + 1
'Next
'For Each there In file_there
'Range("B" & b) = there
'Range("C" & c) = file_there(there)
'b = b + 1
'Next
Range("A2:A" & L & 1) = Application.Transpose(file_missing)
Range("B2:C" & y & 1) = Application.Transpose(file_there)
MsgBox "stop"
End Sub
【问题讨论】:
-
问题太宽泛了。
-
tldr版本就是纠结用什么来抓数据(文件名和文件夹代码),比较和排序最简单
-
将信息写入工作表,然后您可以根据需要进行排序/过滤。你的数据集的实际大小是多少?您需要检查多少个文件/文件夹?不过那里的代码太多了。
-
@dd_tall:好吧,我几乎用字典来处理所有事情,即使一个ciollection 或一个数组就足够了。
-
我会为此使用一个字典。遍历源目录并为您找到的所有内容添加键。然后迭代您找到的比较目录和 remove 键。完成后字典中剩下的所有内容都是您的结果集。
标签: arrays vba sorting dictionary collections