2014-04-03 2 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'을 추가하고 컴파일 ... '끝'경우 커플 '다음'... 어쩌면 더 ... 코드 수정 및 다시 게시 –

+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의 "크기"특성/함수입니다. 예를 들어 크기가 동일한 파일 (즉 관련된 KB 및이 파일의 문자 수)에 대해 다른 크기를 제공합니다. – user3405572

+0

파일의 크기가 다르지 않습니까? 'FileSize'는 바이트 단위로 반환되므로 Windows 탐색기에서 파일 속성을 확인하면 KB 또는 MB로 표시 될 수 있습니다.이 값은 반올림됩니다. 좋은 파일 비교 유틸리티 (예 : WinDiff, VBinDiff 등)를 찾을 수 있습니다.) 두 파일에 대해 실행하십시오. 파일 크기보고가 올바르지 않은 경우가 매우 드물기 때문에 결과를 알려주십시오. –

+0

파일 크기는 FileLen (file1)이 내게 값을주고 그 값이 달라 지므로 문제가 있습니다. 같은 크기의 파일에서 다른 파일로. 같은 문자 수와 공백을 가진 두 파일을 비교하고 있습니다. 나에게 값을주고 비교하기 위해 .Size 함수를 사용해보아야 할 것이다. – user3405572

관련 문제