Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filesystem sort order

Status
Not open for further replies.

cubby

MIS
Sep 19, 2002
6
US
I have a program that reads image files in a folder and converts them to pdf files. Does anyone know a way to control the sort order by date and time or by name of the filesystem object in the -For each file in files collection. My code below:

'loop through tif files
Set fld = fso.GetFolder(sscantemp)
Set tFils = fld.Files
For Each tfil In tFils
...
...
Next

I need to convert the files in some known order because I am combining them into one pdf file along with previously converted files.
Thanks,
Cubby
 
You could chuch them into a recordset and sort them. Here is an example:

Code:
Private Sub Command1_Click()
    Dim fso As FileSystemObject
    Dim fld As Folder
    Dim tfil As File
    Dim tFiles As Files
    Dim rs As ADODB.Recordset

    Set fso = New FileSystemObject
    Set rs = New ADODB.Recordset

    ' Create an empty recordset
    With rs
        .Fields.Append "File Name", adVarChar, 255
        .Fields.Append "Date", adDate
        .CursorType = adOpenStatic
        .LockType = adLockOptimistic
        .Open
    End With

    ' Loop through files in a folder
    Set fld = fso.GetFolder("C:\Test")
    Set tFils = fld.Files
    For Each tfil In tFils
        With rs
            .AddNew
            .Fields("File Name") = tfil
            .Fields("Date") = fso.GetFile(tfil).DateCreated
            .Update
        End With
    Next

    ' Sort the recordset by Date
    rs.Sort = "Date ASC"
    rs.MoveFirst

    ' Loop through sorted recordset
    Do
        MsgBox rs.Fields("File Name").Value & vbCrLf & _
        rs.Fields("Date").Value
        rs.MoveNext
    Loop Until rs.EOF

    ' Closes and destroys objects from memory
    rs.Close
    Set rs = Nothing
    Set tfil = Nothing
    Set tFiles = Nothing
    Set fld = Nothing
    Set fso = Nothing

    ' Prompt user of completion
    MsgBox "Done!", vbInformation
End Sub

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top