Get FileNames from a Folder/Directory

While working on files and folders, many a time I come across a situation where I want the list of all filenames in a specific folder or directory. I tried out a code that helps me a lot. – Selva V Pasupathy

Sub GetFileNames()
Dim sPath As String, sFileNm As String
sPath = “C:DW”
‘You can also use Application.GetOpenFilename to get a file name from a folder,
‘and then extract the Directory name from that string
‘You can also use filters with GetOpenFilenam such as *.txt, see Help on this topic
sFileNm = Dir(sPath, vbNormal) ‘Get the first file from the specified directory
‘Start a loop
Do While sFileNm <> “”
     ‘If the file has a dbf extension then print the file name
     If Right(sFileNm, 3) = “dbf” Then
         Debug.Print sFileNm
     End If
    sFileNm = Dir
Loop
End Sub

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: