2014-04-25 1 views
0

파일 이름을 Excel 시트로 읽으려면 다음 코드를 사용하지만 하위 폴더를 포함하고 전체 파일 경로를 캡처하고 싶습니다. 나는 몇 가지 시도했지만 아무도 일한 적이 없다. 나는 내 상황에서 기능하도록 편집 된 다른 사람들의 코드 조각들로부터 이것을 함께 모으고있다. 불행히도 이것이 나의 이해가 그것이 이루어져야하는만큼 철저하지 않다는 것을 의미한다.폴더 및 하위 폴더에서 Excel로 파일 경로 읽기

Option Explicit 
Sub GetFileNames() 
Dim xRow As Long 
Dim xDirect$, xFname$, InitialFoldr$ 
InitialFoldr$ = "C:\" 

With Application.FileDialog(msoFileDialogFolderPicker) 
.InitialFileName = Application.DefaultFilePath & "\" 
.Title = "Please select the folder to list audio files from" 
.InitialFileName = InitialFoldr$ 
.Show 

    If .SelectedItems.Count <> 0 Then 
    xDirect$ = .SelectedItems(1) & "\" 
    xFname$ = Dir(xDirect$, 7) 
    Do While xFname$ <> "" 
    Worksheets("Metadata").Activate 
    ActiveSheet.Range("A2").Select 
    ActiveCell.Offset(xRow) = xFname$ 
    xRow = xRow + 1 
    xFname$ = Dir 
    Loop 

    Dim x& 
    With Application 
      .ScreenUpdating = False 
      Rows.Hidden = False 
      Rows.Hidden = True 
     For x = 1 To Rows.Count 
      If .WorksheetFunction.CountA(Rows(x)) > 0 Then Rows(x).Hidden = False 
     Next x 
     .ScreenUpdating = False 
    End With 

    Worksheets("Metadata").Visible = True 
    Worksheets("Menu").Visible = False 

End If 
End With 
End Sub 

나는 매우 해요 작가, 제목 등 앨범 :

파일은 오디오 파일 (WAV 또는 MP3) 파일을 태그하는 데 사용되는 메타 데이터를 포함 할 스프레드 시트의 나머지는 VBA에 처음 익숙하지만 VBA의 일부를 이해하기 시작했습니다.

답변

0

이 코드는 폴더와 모든 하위 폴더에서 모든 mp3를 추출합니다. VBA와 함께 행운을 빌어 요!

Public Sub FindFiles() 
'you must add a reference to 'Microsoft Shell Controls And Automation' 

Dim shl As Shell32.Shell 
Dim fol As Shell32.Folder 
Dim row As Long 

Set shl = New Shell32.Shell 
Set fol = shl.Namespace("E:\CDs\") 
row = 1 

ProcessFolderRecursively fol, row 

End Sub 

Private Sub ProcessFolderRecursively(fol As Shell32.Folder, ByRef row As Long) 

Dim item As Shell32.FolderItem 
Dim fol2 As Shell32.Folder 

If Not fol Is Nothing Then 
    For Each item In fol.Items 
     If item.IsFolder Then 
      Set fol2 = item.GetFolder 
      ProcessFolderRecursively fol2, row 
     Else 
      'you might need to edit the criterion here 
      If item.Type = "MP3 Format Sound" Then 
       Cells(row, 1) = item.Path 
       row = row + 1 
      End If 
     End If 
    Next 
End If 

End Sub 
+0

감사하지만 그것은 단지 MP3 파일을 찾을 싶지 않아, 그것은 나에게 폴더의 모든 것을 보여줄 필요가 그대로 완벽하게 작동 MP3 파일, WAVS, aiffs, wmas 등 등의 혼합물이 될 수 루트 폴더 및 파일 이름에 대해서는 이름 대신 하위 폴더 및 파일 경로를보고 약간 더 유연하게 지정하기 만하면됩니다. 파일 이름 만 있으면 경로를 다듬을 수 있습니다. – user3506327

+0

'If item.Type = ...'행과 그 아래의 해당 End If를 제거하면 모든 하위 폴더에있는 모든 파일이 추출됩니다 – steveo40

+0

이 질문에 대한 답변을 얻으려면 답변으로 표시하십시오. 감사합니다 – steveo40

관련 문제