hexagon logo

How Do I List all files in a folder?

I would like to end up with an array that contains all filenames in a specified folder. I've cobbled together several ways to do this in VB from examples on the net, but none of them work with PC-DMIS's Cypress Enable crap.

How would I do this?

And can I grab specific properties of these files? Like Date/Time Modified?

Thanks!
  • A fast hack that lists the filenames of all bitmaps in C:\Windows.
    Edit the path and filemask to suit your needs.

    I leave the other properties (modified, timestamp etc.) for you to code.

    [COLOR="YellowGreen"]' Simple filearray
    '
    ' vpt.se 2010[/COLOR]
    
    
    Sub Main()
    Dim FileArray() As String
    Dim FileName As String
    
    [COLOR="YellowGreen"]' Setup Folder And filecounter 
    ' Example lists all bitmaps In C:\Windows[/COLOR]
    Folder = Dir("C:\Windows\*.bmp")
    count = 0
    
    [COLOR="yellowgreen"]' Parse the folder Until filename returns empty (no more files)
    ' And count the files ending With .bmp[/COLOR]
    While Folder <> ""
        count = count + 1
        Folder = Dir
    Wend
    
    [COLOR="yellowgreen"]' The MsgBox is For debug/example purposes And displays the
    ' number of bitmaps found[/COLOR]
    MsgBox "Filecount: " & count
    [COLOR="yellowgreen"]' Initialize the FileArray (create the array) With the size of
    ' count (number of files found)[/COLOR]
    ReDim FileArray(count)
    [COLOR="yellowgreen"]' Fill the filearray With the bitmap filenames[/COLOR]
    FileArray(0) = Dir("C:\Windows\*.bmp")
    
    [COLOR="yellowgreen"]' For debug/example purposes, displays the bitmap filenames[/COLOR]
    For i = 1 To count
        FileArray(i) = Dir
        FileName = FileName + Chr(13) + FileArray(i)
    Next i
    MsgBox FileName
    
    End Sub
    


    Enjoy!

    EDIT: The array FileArray contains the filenames.
  • I added a bit of code to the above example to create a simple text file with all the file names that you can open in notepad or Wordpad. It will put the text file in the same folder that you set in the code for the "Folder" variable.

    I also have a microsoft excel add-in that I can post that allows you to get all the filenames and some of the other file info like size and date modified, etc.


    ' Simple filearray
    '
    ' vpt.se 2010


    Sub Main()
    Dim FileArray() As String
    Dim FileName As String
    Dim FullPath As String
    Dim unit%


    ' Setup Folder And filecounter
    ' Example lists all bitmaps In C:\Windows
    Folder = Dir("C:\Windows\*.bmp")
    FullPath = Left(Folder,InStrRev(Folder,"\"))
    count = 0

    ' Parse the folder Until filename returns empty (no more files)
    ' And count the files ending With .bmp
    While Folder <> ""
    count = count + 1
    Folder = Dir
    Wend

    ' The MsgBox is For debug/example purposes And displays the
    ' number of bitmaps found
    MsgBox "Filecount: " & count
    ' Initialize the FileArray (create the array) With the size of
    ' count (number of files found)
    ReDim FileArray(count)
    ' Fill the filearray With the bitmap filenames
    FileArray(0) = Dir("C:\Windows\*.bmp")

    ' For debug/example purposes, displays the bitmap filenames
    For i = 1 To count
    FileArray(i) = Dir
    FileName = FileName + Chr(13) + FileArray(i)
    Next i
    MsgBox FileName

    unit% = FreeFile
    If Dir(FullPath & "filelist.txt", vbNormal) <> "" Then
    Kill FullPath & "filelist.txt"
    End If
    Open FullPath & "filelist.txt" For Output As unit%
    Print #unit%, FileName
    Close unit%

    End Sub
  • For those of you looking to get file properties:


    Sub Main()
    Dim filespec As String
    filespec = "C:\TEST.TXT"
    Dim fs, f, s
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(filespec)
    s = UCase(filespec) & vbCrLf
    s = s & "Created: " & f.DateCreated & vbCrLf
    s = s & "Last Accessed: " & f.DateLastAccessed & vbCrLf
    s = s & "Last Modified: " & f.DateLastModified
    MsgBox s, 0, "File Access Info"
    End Sub
  • Chally,
    I believe that is how I retrieved the file info in the MS Excel Addin I will post later. I need to look it over first because with any add-in I create, I often experiment or add functionality that isn't needed or wanted by the average user.

    The addin lets you simply copy the path of the folder you want to cell A1 and then click a button and it grabs all the file names, file sizes, and the last modified date. It populates the spreadsheet with the info in rows. It's pretty cool.
  • Cool stuff. If you have any more little bits like that, you should post them as well. Always useful.