这个代码有一些麻烦。它实际上是通过循环目录/文件夹中的所有文件来为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