2014-04-03 57 views
2

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

你好,我对这段代码有些麻烦。它实际上是通过循环目录/文件夹中的所有文件来为dir1array(ctr1)和dir2array(ctr2)分配值;有没有办法让这个数组工作?感谢您的帮助。 – user3405572

+2

在模块顶部添加'Option Explicit'并编译...您错过了'End If'一对'Next'...也许更多...修复并重新发布您的代码.. –

+0

我转贴全部现在编码。正如你所看到的,我在代码的最后还有一个与bytArr1(lCtr)和byArr2(lCtr)数组类似的问题。 – user3405572

回答

1

以下是您的代码的变体,但不使用数组。

Option Explicit 

Sub findMatchFilenames() 
Dim lLen1  As Long 
Dim lLen2  As Long 

Dim oFSO  As New Scripting.FileSystemObject 
Dim dir1  As Folder 
Dim dir2  As Folder 
Dim oFile1  As File 
Dim oFile2  As File 
Dim strFolder1 As String 
Dim strFolder2 As String 

    Close #1 ' I always close first when testing (in case I don't get to normal close) 
    Close #2 
    Open "C:\Temp\" & Format(Now(), "_SAME_yyyy-mm-dd_hh-mm") & ".txt" For Output As #1 
    Open "C:\Temp\" & Format(Now(), "_Different_yyyy-mm-dd_hh-mm") & ".txt" For Output As #2 

    Set oFSO = New FileSystemObject 
    strFolder1 = "c:\Temp\" 
    strFolder2 = "c:\Tempo\" 

    Set dir1 = oFSO.GetFolder(strFolder1) 
    Set dir2 = oFSO.GetFolder(strFolder2) 

    For Each oFile1 In dir1.Files 
     If oFSO.FileExists(strFolder2 & oFile1.Name) Then  ' If it matches same name 
      Set oFile2 = oFSO.GetFile(strFolder2 & oFile1.Name) 
      If oFile1.Size = oFile2.Size Then 
       Print #1, oFile1.Name & vbTab & "File found in both folders; Size is the same;" 
       Debug.Print oFile1.Name & vbTab & "File found in both folders; Size is the same;" 
      Else 
       Print #1, oFile1.Name & vbTab & "Found in both folders; Size is DIFFERENT; " & oFile1.Size & " vs: " & oFile2.Size 
       Debug.Print oFile1.Name & vbTab & "Found in both folders; Size is DIFFERENT; " & oFile1.Size & " vs: " & oFile2.Size 
      End If 
     Else    ' Same file not found. 
      Debug.Print "File not present in 2nd folder: " & oFile1.Name 
      Print #1, oFile1.Name & vbTab & "File NOT found in second folder;" 
     End If 

    Next oFile1 
    Set oFile1 = Nothing 
    Set oFile2 = Nothing 
    Set oFSO = Nothing 

    Close #1 
    Close #2 

End Sub 
+0

谢谢你的时间。我正在查看你的代码的结果,它看起来不错。有一件事我有点麻烦,它是oFile1.Size中的“size”属性/函数。例如,它为具有相同大小的文件(即所涉及的KB以及该文件中的字符数)提供了不同的大小。 – user3405572

+0

你确定这些文件大小不一样吗? 'FileSize'是以字节为单位返回的,因此如果您碰巧在Windows资源管理器中查看文件属性,则可以用KB或MB表示 - 这是一个四舍五入的数字。我会找到一个很好的文件比较实用程序(即WinDiff,VBinDiff等)。)并针对这两个文件运行。请让我知道结果,因为它是非常不寻常的文件大小报告不正确.. –

+0

关于文件大小,问题是与FileLen(file1),因为它应该给我一个值,并且该值不同从一个相同大小的文件到另一个。我正在比较两个文件之间具有相同数量的字符和空格。我不得不尝试使用.Size函数给我一个值和比较。 – user3405572

相关问题