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!
Parents
  • 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
Reply
  • 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
Children
No Data