Find and List all Files and Folders in a Directory

In this article I will explain how you can  find and create a list of all the files and folder in a directory:

Jump To:

Get List of All Files Using, FileSystemObject:

In this example I will assume that I have the following files in the directory “D:StuffFreelancesWebsiteBlogArraysPics”:

Files In Directory

The code below retrieves the file in this directory and creates a list of their names and paths:

Sub Example1()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:StuffFreelancesWebsiteBlogArraysPics")
i = 1
'loops through each file in the directory and prints their names and path
For Each objFile In objFolder.Files
    'print file name
    Cells(i + 1, 1) = objFile.Name
    'print file path
    Cells(i + 1, 2) = objFile.Path
    i = i + 1
Next objFile
End Sub

Result:
File Names and Path Excel VBA

Get List of All Folders Using, FileSystemObject:

In this example I will use the directory “D:StuffFreelancesWebsiteBlog”. The following folders can be found in that directory:

Folders in Path

Using the code below, the names of the folders and their associated paths are listed on column A and B:

Sub Example2()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer

'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("D:StuffFreelancesWebsiteBlog")
i = 1
'loops through each file in the directory and prints their names and path
For Each objSubFolder In objFolder.subfolders
    'print folder name
    Cells(i + 1, 1) = objSubFolder.Name
    'print folder path
    Cells(i + 1, 2) = objSubFolder.Path
    i = i + 1
Next objSubFolder
End Sub

Result:
List of Folder Names and Paths Excel VBA

Dir():

Using the Dir() function you can get the list of files and folders in a specific path. The Dir() function takes 2 input parameters, the directory path and the type of file we are looking for:

strFileOrFolder = Dir(strPath, FileAttribute)

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

strPath is the path of the directory which the files and folder are in.  The FileAttribute specifies what type of file or folder we are after. For more information about the FileAttribute parameter click here.

Get List of All Files in a Directory Using Dir():

In this example I will assume that I have the following files in the directory “D:StuffFreelancesWebsiteBlogArraysPics”:

Files In Directory

The code below retrieves the name and path of the files  in that directory using the DIR() function and lists them in column A and B:

Sub Example3()

Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String

strDirectory = "D:StuffFreelancesWebsiteBlogArraysPics"
i = 1
flag = True
varDirectory = Dir(strDirectory, vbNormal)

While flag = True
    If varDirectory = "" Then
        flag = False
    Else
        Cells(i + 1, 1) = varDirectory
        Cells(i + 1, 2) = strDirectory + varDirectory
        'returns the next file or directory in the path
        varDirectory = Dir
        i = i + 1
    End If
Wend

Result:
File Names and Path Excel VBA

Get List of All Folders and Files in a Directory Using Dir():

In this example I will assume that I have the following files and  folders in the directory “D:StuffFreelancesWebsiteBlog”:

Folders in Path

The code below retrieves the name and path of the folders and files in that directory using the DIR() function and lists them in column A and B:

Sub Example4()

Dim varDirectory As Variant
Dim flag As Boolean
Dim i As Integer
Dim strDirectory As String

strDirectory = "D:StuffFreelancesWebsiteBlog"
i = 1
flag = True
varDirectory = Dir(strDirectory, vbDirectory)

While flag = True
    If varDirectory = "" Then
        flag = False
    Else
        Cells(i + 1, 1) = varDirectory
        Cells(i + 1, 2) = strDirectory + varDirectory
        'returns the next file or directory in the path
        varDirectory = Dir
        i = i + 1
    End If
Wend

Result:
List of Folders and File Names and Paths DIR Excel VBA

[출처] http://software-solutions-online.com/list-files-and-folders-in-a-directory/

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
1220 [一日30分 인생승리의 학습법] Qiskit 시작하기 (Getting Started with Qiskit) file 졸리운_곰 2025.06.03 17
1219 [一日30分 인생승리의 학습법] 양자컴퓨팅 프로그래밍 file 졸리운_곰 2025.06.03 12
1218 [一日30分 인생승리의 학습법] [Git] 다중 리모트를 사용하여 여러 Git 연동하기(Gitea, GitHub) file 졸리운_곰 2025.05.25 7
1217 [一日30分 인생승리의 학습법] [GitHub][terminal] 비밀번호 인증 에러를 토큰으로 해결하고 로그인 하기 file 졸리운_곰 2025.05.24 32
1216 [一日30分 인생승리의 학습법] [알아봅시다] 블록체인 게임들의 가능성과 미래 file 졸리운_곰 2025.04.08 29
1215 이 어지러운시대의 극복법 만화보기 file unmask 2025.04.08 56
1214 [ 一日30分 인생승리의 학습법] IT 국비교육, 쓰레기 속에서 그나마 덜 쓰레기인 곳 찾는 팁 file 졸리운_곰 2025.03.08 22
1213 [ 一日30分 인생승리의 학습법] 소프트웨어 개발하다보면 "connection reset" 등, 소프트웨어 버그 적인 문제가아닌 하드웨어나 네트워크 오류 메시지의 예 file 졸리운_곰 2025.03.01 23
1212 [ 一日30分 인생승리의 학습법] 기술부채(Technical Debt)는 소프트웨어 개발이나 프로젝트 과정에서, 약속된 것과 실제로 제공된 것 사이에 차이가 발생하는 것을 의미합니다. file 졸리운_곰 2025.01.23 32
1211 [ 一日30分 인생승리의 학습법] 고가용성(High Availability) 시스템을 위한 5가지 전략 file 졸리운_곰 2024.12.28 34
1210 [ 一日30分 인생승리의 학습법] 켈리 공식을 간단히 투자해 적용해 보자 - 켈리 크라이티리언과 확률적 사고의 중요성 file 졸리운_곰 2024.12.26 36
1209 [ 一日30分 인생승리의 학습법] [markdown] mermaid를 이용해서 UML 그리기 - 플로우차트 file 졸리운_곰 2024.12.01 50
1208 [ 一日30分 인생승리의 학습법] Mermaid.js 정리???????? file 졸리운_곰 2024.12.01 69
1207 [ 一日30分 인생승리의 학습법] Mermaid를 이용한 시퀀스 다이어그램 file 졸리운_곰 2024.12.01 34
1206 [ 一日30分 인생승리의 학습법] Mermaid - 코드로 순서도(flowchart) 그리기 file 졸리운_곰 2024.12.01 30
1205 [ 一日30分 인생승리의 학습법] 유니코드 그래픽 기호(심벌) Huge List of Unicode Symbols 졸리운_곰 2024.07.31 48
1204 [ 一日30分 인생승리의 학습법] PocketBase Attempt to simplify the serve command for prod : 포켓베이스 프로덕션 포트 도메인 네임 설정 졸리운_곰 2024.06.10 73
1203 [ 一日30分 인생승리의 학습법] google spreadsheet app script 로 코인 현황 : 거래소 API 접근할 때 알아두면 좋은 함수 file 졸리운_곰 2024.06.08 65
1202 [ 一日30分 인생승리의 학습법] 매크로 프로그램 정리 졸리운_곰 2024.06.08 90
1201 [ 一日30分 인생승리의 학습법] 스마트스토어 vs 아임웹 vs 카페24 file 졸리운_곰 2024.05.16 79
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED