List Files In Directory

A simple program for listing all files in a certain directory by calling a VBA Macro Code.

Explanation

The program is set up by giving input about which folder/directory that the program shall analyse. The VBA program then uses the Dir function to get the information about what files are stored in the folder/directory. Then the program simply writes the data to the worksheet. It is possible to use the data in an array if wanting to modify and use the code in an other program. This is a good function when you need to write something or perform an operation to all files stored in a certain folder but you do not know exactly what the files are called or how many they are.

The entire VBA Excel program is available for downloading at the bottom of this page, enjoy!
 

 

Code

Public Sub List_Files_In_Directory()

Range("A5:A2000").ClearContents

Dim List_Files_In_Directory(10000, 1)
Dim One_File_List   As String
Dim Number_Of_Files_In_Directory As Long

One_File_List = Dir$("C:" + "\*.*")
Do While One_File_List <> ""
    List_Files_In_Directory(Number_Of_Files_In_Directory, 0) = One_File_List
    One_File_List = Dir$
    Number_Of_Files_In_Directory = Number_Of_Files_In_Directory + 1
Loop

Number_Of_Files_In_Directory = 0
While List_Files_In_Directory(Number_Of_Files_In_Directory, 0) <> tom
    Range("A5").Offset(Number_Of_Files_In_Directory, 0).Value = List_Files_In_Directory(Number_Of_Files_In_Directory, 0)
    Number_Of_Files_In_Directory = Number_Of_Files_In_Directory + 1
Wend

End Sub

 

 

 

Download excel file! List_All_Files_In_Directory.xls

 
Comments (5)
Thanks!!!
5 Tuesday, 13 December 2011 16:52
Mary
This was a great reminder. Thanks for posting.
file list
4 Monday, 24 October 2011 12:13
mike
thanks - this helped me a lot , yes some tweaks that can be done to smarten code up, but I can do those

thanks for the post
List files in directory
3 Thursday, 26 May 2011 23:04
TQT
I don'tknow about stupic? Works pretty well to me with some modification. Thanks.
stupic code
2 Sunday, 02 January 2011 01:29
wtf
worst piece of code ever posted. you are retarded!
tom and jerry animation
1 Saturday, 03 April 2010 16:12
tom and jerry
Tom and jerry animation at:
http://tomandjerryanimation.blogspot.com

Add your comment

Your name:
Subject:
Comment: