這個代碼有一些麻煩。它實際上是通過循環目錄/文件夾中的所有文件來爲dir1array(ctr1)和dir2array(ctr2)分配值;有沒有辦法讓這個數組工作?如何使用數組訪問vba文件夾中的文件?
Option Explicit
'*********************************************************************
'* Verify if files have the same name before proceeding to compare *
'* their length *
'* DYNAMIC ARRAYs *
'*********************************************************************
Sub findMatchFilenames()
'Dim fso As fileSystemObject
Dim objMapinfo
Dim fso As New Scripting.FileSystemObject
Dim dir1 As Folder
Dim dir2 As Folder
Dim file1 As File
Dim file2 As File
Dim dir1array() As String
Dim dir2array() As String
ReDim dir1array(0 To 100) As String
ReDim dir2array(0 To 100) As String
Dim ctr1 As Integer
Dim ctr2 As Integer
Dim lLen1 As Long, lLen2 As Long
Dim myFile As String, text As String, textline As String
Set fso = New FileSystemObject
Set dir1 = fso.GetFolder("c:\Temp\")
Set dir2 = fso.GetFolder("c:\Tempo\")
ctr1 = 0
For Each file1 In dir1.Files
ctr2 = 0
For Each file2 In dir2.Files
dir1array(ctr1) = file1.Name
dir2array(ctr2) = file2.Name
If dir1array(ctr1) = dir2array(ctr2) Then
MsgBox "" & dir1array(ctr1) & "" & dir2array(ctr2)
Debug.Print file1.Name & " matches " & file2.Name
lLen1 = FileLen(file1)
lLen2 = FileLen(file2)
If lLen1 <> lLen2 Then
Exit Sub
Else
MsgBox "The files have the same length"
End If
End If
ctr2 = ctr2 + 1
Next file2
ctr1 = ctr1 + 1
Next file1
Close #1
End Sub
你好,我對這段代碼有些麻煩。它實際上是通過循環目錄/文件夾中的所有文件來爲dir1array(ctr1)和dir2array(ctr2)分配值;有沒有辦法讓這個數組工作?感謝您的幫助。 – user3405572
在模塊頂部添加'Option Explicit'並編譯...您錯過了'End If'一對'Next'...也許更多...修復並重新發布您的代碼.. –
我轉貼全部現在編碼。正如你所看到的,我在代碼的最後還有一個與bytArr1(lCtr)和byArr2(lCtr)數組類似的問題。 – user3405572