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/

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
478 1조원 짜리 앱 아이디어 file 졸리운_곰 2016.12.05 198
» Find and List all Files and Folders in a Directory file 졸리운_곰 2016.12.04 107
476 Rest Api 이해와 설계 및 개발 file 졸리운_곰 2016.11.28 270
475 우분투(Ubuntu) 서버 네트워크 설정 졸리운_곰 2016.11.26 85
474 말뫼(Malmo) 사용법 Tutorial file 졸리운_곰 2016.11.20 88
473 "마인크래프트로 AI 연구"…MS 오픈소스 프로젝트 공개 file 졸리운_곰 2016.11.20 126
472 마인크래프트 대체용 10가지 오픈소스 게임, 10 open source alternatives to Minecraft file 졸리운_곰 2016.11.13 274
471 App기획 [앱개발 프로세스] 졸리운_곰 2016.10.27 257
470 인공지능 기술자를 위한 파이썬 특강 (v0.2)의 사본.docx file 졸리운_곰 2016.10.27 142
469 프로그래밍 언어별 딥러닝 라이브러리 정리 file 졸리운_곰 2016.10.27 123
468 인공지능언어 프롤로그 프로그래밍 prolog.pdf file 졸리운_곰 2016.10.27 298
467 [엑셀로 웹 스크래핑3] Excel VBA Web Scraping Tutorial file 졸리운_곰 2016.10.09 302
466 [엑셀로 웹 스크래핑2] Scrape webpage data using Excel & VBA 졸리운_곰 2016.10.09 441
465 [엑셀로 웹 스크래핑] Web Scraping - Basics 졸리운_곰 2016.10.09 374
464 Web Scraping Tutorial by ms excel 엑셀로 웹 스크래핑 file 졸리운_곰 2016.10.09 936
463 게임기획자를 위한 엑셀VBA 강좌 자료 파일 업로드 file 졸리운_곰 2016.10.03 129
462 마크다운에 관하여 file 졸리운_곰 2016.08.28 224
461 마크다운(Markdown)으로 글을 써보자 file 졸리운_곰 2016.08.28 225
460 Windows에서 텐서플로우 (tensorFlow)설치 file 졸리운_곰 2016.08.21 745
459 Top 50 open source web crawlers for data mining 졸리운_곰 2016.08.10 141
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED