Files collection
폴더에 있는 모든 File 개체의 컬렉션입니다.
Count Property
폴더 내에 있는 파일 수를 반환합니다.
다음 코드는 Count 속성을 사용하는 예제입니다.
Sub TestCountFilesInFolder()
Dim folderPath As String
Dim fileCount As Long
' 파일 개수를 세고자 하는 폴더 경로 지정
folderPath = "C:\Example"
' 함수 호출하여 파일 개수 가져오기
fileCount = GetFileCountInFolder(folderPath)
' 파일 개수 표시
MsgBox folderPath & " 폴더 내 파일 개수: " & fileCount
End Sub
Function GetFileCountInFolder(ByVal folderPath As String) As Long
Dim fso As Object
Dim filesCollection As Object
' FileSystemObject 생성
Set fso = CreateObject("Scripting.FileSystemObject")
' 폴더가 존재하는지 확인
If fso.FolderExists(folderPath) Then
' 지정한 폴더의 Files 컬렉션 가져오기
Set filesCollection = fso.GetFolder(folderPath).Files
' 폴더 내 파일 개수 반환
GetFileCountInFolder = filesCollection.Count
Else
' 폴더가 존재하지 않음을 표시하기 위해 -1 반환
GetFileCountInFolder = -1
End If
' FileSystemObject 정리 및 해제
Set filesCollection = Nothing
Set fso = Nothing
End Function
Item Property
컬렉션에서 특정 파일을 참조합니다.
다음 코드는 Item 속성을 사용하는 예제입니다.
예제 코드 - 1
Sub AccessFilesCollectionWithoutForEach()
Dim fso As Object
Dim folderPath As String
Dim filesCollection As Object
Dim file As Object
Dim i As Integer
' Late Binding을 사용하여 FileSystemObject 생성
Set fso = CreateObject("Scripting.FileSystemObject")
' 검색할 폴더 경로 지정
folderPath = "D:\download"
Set filesCollection = fso.GetFolder(folderPath).Files
' "Northwind.mdb" 파일 항목에 접근하여 정보 출력
With filesCollection.Item("Northwind.mdb")
Debug.Print "파일 크기: " & FormatFileSize(.Size)
Debug.Print "파일 경로: " & .path
Debug.Print "파일 유형: " & .Type
End With
Set filesCollection = Nothing
Set fso = Nothing
End Sub
Function FormatFileSize(ByVal bytes As Double) As String
Dim sizes() As String
sizes = Split("Bytes|KB|MB|GB|TB", "|")
Dim i As Integer
For i = 0 To UBound(sizes) - 1
If bytes >= 1024 Then
bytes = bytes / 1024
Else
Exit For
End If
Next i
FormatFileSize = FormatNumber(bytes, 2) & " " & sizes(i)
End Function
예제 코드 - 2
Sub TestGetFileInfoUsingItem()
Dim folderPath As String
' 작업할 폴더 경로 지정
folderPath = "C:\Example"
' Item 속성을 사용하여 파일 정보 가져오는 함수 호출
GetAndDisplayFileInfo folderPath
End Sub
Sub GetAndDisplayFileInfo(ByVal folderPath As String)
Dim fso As Object
Dim filesCollection As Object
Dim file As Object
' FileSystemObject 생성
Set fso = CreateObject("Scripting.FileSystemObject")
' 폴더가 존재하는지 확인
If fso.FolderExists(folderPath) Then
' 지정한 폴더의 Files 컬렉션 가져오기
Set filesCollection = fso.GetFolder(folderPath).Files
' 컬렉션 내 각 파일에 대해 반복
For Each file In filesCollection
' 파일 정보를 출력하는 함수 호출
DisplayFileInfo file, filesCollection
Next file
Else
MsgBox "폴더가 존재하지 않습니다: " & folderPath
End If
' FileSystemObject 정리 및 해제
Set filesCollection = Nothing
Set fso = Nothing
End Sub
Sub DisplayFileInfo(ByVal file As Object, ByVal filesCollection As Object)
' Item 속성을 사용하여 컬렉션 내의 파일 참조
Debug.Print "파일 이름: " & filesCollection.Item(file.name).name
Debug.Print "크기: " & filesCollection.Item(file.name).Size & " 바이트"
Debug.Print "마지막 수정일: " & filesCollection.Item(file.name).DateLastModified
Debug.Print "---"
End Sub
위 예제는 Item 속성을 사용하는 법을 알아보기 위해 만든 코드입니다.
다음 코드는 Item 속성을 삭제한 코드입니다.
Sub TestGetFileInfoUsingItem()
Dim folderPath As String
' 작업할 폴더 경로 지정
folderPath = "C:\Example"
' 파일 정보 가져오는 함수 호출
GetAndDisplayFileInfo folderPath
End Sub
Sub GetAndDisplayFileInfo(ByVal folderPath As String)
Dim fso As Object
Dim filesCollection As Object
Dim file As Object
' FileSystemObject 생성
Set fso = CreateObject("Scripting.FileSystemObject")
' 폴더가 존재하는지 확인
If fso.FolderExists(folderPath) Then
' 지정한 폴더의 Files 컬렉션 가져오기
Set filesCollection = fso.GetFolder(folderPath).Files
' 컬렉션 내 각 파일에 대해 반복
For Each file In filesCollection
' 파일 정보를 출력하는 함수 호출
DisplayFileInfo file
Next file
Else
MsgBox "폴더가 존재하지 않습니다: " & folderPath
End If
' FileSystemObject 정리 및 해제
Set filesCollection = Nothing
Set fso = Nothing
End Sub
Sub DisplayFileInfo(ByVal file As Object)
' 파일 정보 출력
Debug.Print "파일 이름: " & file.name
Debug.Print "크기: " & file.Size & " 바이트"
Debug.Print "마지막 수정일: " & file.DateLastModified
Debug.Print "---"
End Sub
'Microsoft Scripting Runtime' 카테고리의 다른 글
TextStream 개체 (0) | 2023.08.14 |
---|---|
File 개체 (0) | 2023.08.13 |
Folder 개체 (0) | 2023.08.10 |
Folders 컬렉션 (0) | 2023.08.10 |
Drive 개체 (0) | 2023.08.10 |