이 기사에서는 폴더 내의 모든 파일에 대한 세부 정보를 수집하는 매크로를 만듭니다.

매크로를 실행하기 전에 텍스트 상자에 폴더의 경로를 지정해야합니다.

ArrowMain

매크로를 실행하면 폴더 내 모든 파일의 파일 이름, 파일 경로, 파일 크기, 만든 날짜 및 마지막으로 수정 한 날짜를 반환합니다.

ArrowOutput

논리 설명

이 기사에서는“ListFilesInFolder”와“TestListFilesInFolder”라는 두 개의 매크로를 만들었습니다.

“ListFilesInFolder”매크로는 폴더 내의 모든 파일과 관련된 세부 정보를 표시합니다.

“TestListFilesInFolder”매크로는 헤더를 지정하고 “ListFilesInFolder”매크로를 호출하는 데 사용됩니다.

코드 설명

FSO = CreateObject ( “Scripting.FileSystemObject”)

위 코드는 파일 시스템 개체의 새 개체를 만드는 데 사용됩니다.

SourceFolder = FSO.GetFolder (SourceFolderName) 설정

위의 코드는 경로로 지정된 폴더의 개체를 만드는 데 사용됩니다.

Cells (r, 1) .Formula = FileItem.Name Cells (r, 2) .Formula = FileItem.Path Cells (r, 3) .Formula = FileItem.Size Cells (r, 4) .Formula = FileItem.DateCreated Cells ( r, 5) .Formula = FileItem.DateLastModified 위의 코드는 파일의 세부 정보를 추출하는 데 사용됩니다.

SourceFolder.SubFolders의 각 하위 폴더에 대해 ‘하위 폴더에 대해 동일한 절차 호출 ListFilesInFolder SubFolder.Path, True Next SubFolder 위 코드는 하위 폴더 내의 모든 파일에 대한 세부 정보를 추출하는 데 사용됩니다.

Columns ( “A : E”). Select Selection.ClearContents 위 코드는 열 A에서 E까지의 내용을 삭제하는 데 사용됩니다.

아래 코드를 따르세요

Option Explicit

Sub ListFilesInFolder(ByVal SourceFolderName As String, ByVal IncludeSubfolders As Boolean)

'Declaring variables

Dim FSO As Object

Dim SourceFolder As Object

Dim SubFolder As Object

Dim FileItem As Object

Dim r As Long



'Creating object of FileSystemObject

Set FSO = CreateObject("Scripting.FileSystemObject")

Set SourceFolder = FSO.GetFolder(SourceFolderName)

r = Range("A65536").End(xlUp).Row + 1

For Each FileItem In SourceFolder.Files

'Display file properties

Cells(r, 1).Formula = FileItem.Name

Cells(r, 2).Formula = FileItem.Path

Cells(r, 3).Formula = FileItem.Size

Cells(r, 4).Formula = FileItem.DateCreated

Cells(r, 5).Formula = FileItem.DateLastModified



r = r + 1



Next FileItem

'Getting files in sub folders

If IncludeSubfolders Then

For Each SubFolder In SourceFolder.SubFolders

'Calling same procedure for sub folders

ListFilesInFolder SubFolder.Path, True

Next SubFolder

End If

Set FileItem = Nothing

Set SourceFolder = Nothing

Set FSO = Nothing

ActiveWorkbook.Saved = True

End Sub

Sub TestListFilesInFolder()

'Declaring variable

Dim FolderPath As String

'Disabling screen updates

Application.ScreenUpdating = False

'Getting the folder path from text box

FolderPath = Sheet1.TextBox1.Value

ActiveSheet.Activate

'Clearing the content from columns A:E

Columns("A:E").Select

Selection.ClearContents

'Adding headers

Range("A14").Formula = "File Name:"

Range("B14").Formula = "Path:"

Range("C14").Formula = "File Size:"

Range("D14").Formula = "Date Created:"

Range("E14").Formula = "Date Last Modified:"

'Formating of the headers

Range("A14:E14").Font.Bold = True

'Calling ListFilesInFolder macro

ListFilesInFolder FolderPath, True

'Auto adjusting the size of the columns

Columns("A:E").Select

Selection.Columns.AutoFit

Range("A1").Select

End Sub

이 블로그가 마음에 들면 Facebook 및 Facebook에서 친구들과 공유하십시오.

여러분의 의견을 듣고 싶습니다. 작업을 개선하고 더 나은 서비스를 제공 할 수있는 방법을 알려주십시오. [email protected]로 문의 해주세요