2014-04-03 97 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

相關問題