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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import files in Order of appearance 1

Status
Not open for further replies.

proximity

Technical User
Sep 19, 2002
132
GB
Hi,

I have a directory of files that are always in this format:
123456.txt
845133.txt
123545.txt

I can import them quite happily into my database without issues. However, the files get imported based on their modified date. I want to import them in the order they appear in the directoy or by ascending numerical order.

So, the above would be imported in this order:
123456.txt
123545.txt
845133.txt

Any ideas gratefully recived :)

Thanks,

--
SM
 
Pass the filenames for each file in the required folder into an array and then sort the array.

Below is some code I use to sort the array on DateLastModified but you will be able to eaily amend to sort the filename

Code:
Dim arrFiles As Variant
Dim Temp    As Variant
Dim i   As Integer
Dim fso As FileSystemObject
Dim fld As Folder
Dim fle As File
Dim FleCount As Integer

Set fso = New FileSystemObject
Set fld = fso.GetFolder(fldPath)

ReDim arrFiles(2, 1)

For Each fle In fld.Files
    FleCount = FleCount + 1
    ReDim Preserve arrFiles(2, FleCount)
    arrFiles(1, FleCount) = fle.Name
    arrFiles(2, FleCount) = fle.DateLastModified
Next fle

If FleCount > 0 Then
    For i = 1 To UBound(arrFiles, 2)
        For j = i + 1 To UBound(arrFiles, 2)
            If (arrFiles(2, j) < arrFiles(2, i)) Then
                Temp = arrFiles(1, i)
                arrFiles(1, i) = arrFiles(1, j)
                arrFiles(1, j) = Temp
                Temp = arrFiles(2, i)
                arrFiles(2, i) = arrFiles(2, j)
                arrFiles(2, j) = Temp
            End If
       Next j
    Next i
    
    For i = 1 To UBound(arrFiles, 2)

        Do Your Stuff Here

    Next i
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top