T
Trefor
I have read through the various posting trying to get this sorted and found a
bubble sort from Tom Ogilvy, but its not working. Can somene work out what I
may have done wrong? The full code is below:
Private FileNameList(1 To 20, 1 To 2), FileListCount As Integer
Sub Test_File_Date_Sort()
Call GetFileList("C:\temp", FileNameList, FileListCount)
For x = 1 To FileListCount
Cells(x + 2 + FileListCount, "A").Value = FileNameList(x, 2)
Cells(x + 2 + FileListCount, "B").Value = FileNameList(x, 1)
Next
End Sub
Sub GetFileList(GF_Folder As String, FileNameList, FileListCount)
' Complile a list of files in a folder that are sorted oldest to newest
Dim fso As Object
Dim folder As Object
Dim File As Variant
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(GF_Folder)
FileListCount = 0
' Load the array with the file name and the date for each file found in
the specific directory
If folder.Files.Count > 0 Then
For Each File In folder.Files
FileListCount = FileListCount + 1
FileNameList(FileListCount, 1) = File
FileNameList(FileListCount, 2) = FileDateTime(File)
Cells(FileListCount, "A").Value = FileNameList(FileListCount, 2)
Cells(FileListCount, "B").Value = FileNameList(FileListCount, 1)
Next File
End If
SortArray (FileNameList)
End Sub
Function SortArray(myArray)
' Tom Ogilvy bubble sort (modified)
' This sort simply takes a 2 dimentional and sorts the value install in
the second element
Dim temp, i, j, k
For i = LBound(myArray, 1) To UBound(myArray, 1) - 1
For j = i + 1 To UBound(myArray, 1)
If myArray(i, 2) > myArray(j, 2) Then
For k = LBound(myArray, 2) To UBound(myArray, 2)
temp = myArray(i, k)
myArray(i, k) = myArray(j, k)
myArray(j, k) = temp
Next
End If
Next
Next
SortArray = myArray
End Function
This is the ouput which suggests that no sort happened at all???
2008 05 02 13:48 file1
2008 05 06 15:08 file2
2008 05 06 15:09 file3
2008 05 06 15:25 file4
2008 05 02 13:45 file5
2008 04 30 08:27 file6
2008 04 30 09:10 file7
2008 05 01 13:00 file8
2008 05 02 13:44 file9
2008 05 02 13:38 file10
2008 05 02 13:40 file11
2008 04 30 08:13 file12
2008 05 02 13:48 file1
2008 05 06 15:08 file2
2008 05 06 15:09 file3
2008 05 06 15:25 file4
2008 05 02 13:45 file5
2008 04 30 08:27 file6
2008 04 30 09:10 file7
2008 05 01 13:00 file8
2008 05 02 13:44 file9
2008 05 02 13:38 file10
2008 05 02 13:40 file11
2008 04 30 08:13 file12
Any ideas?
bubble sort from Tom Ogilvy, but its not working. Can somene work out what I
may have done wrong? The full code is below:
Private FileNameList(1 To 20, 1 To 2), FileListCount As Integer
Sub Test_File_Date_Sort()
Call GetFileList("C:\temp", FileNameList, FileListCount)
For x = 1 To FileListCount
Cells(x + 2 + FileListCount, "A").Value = FileNameList(x, 2)
Cells(x + 2 + FileListCount, "B").Value = FileNameList(x, 1)
Next
End Sub
Sub GetFileList(GF_Folder As String, FileNameList, FileListCount)
' Complile a list of files in a folder that are sorted oldest to newest
Dim fso As Object
Dim folder As Object
Dim File As Variant
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(GF_Folder)
FileListCount = 0
' Load the array with the file name and the date for each file found in
the specific directory
If folder.Files.Count > 0 Then
For Each File In folder.Files
FileListCount = FileListCount + 1
FileNameList(FileListCount, 1) = File
FileNameList(FileListCount, 2) = FileDateTime(File)
Cells(FileListCount, "A").Value = FileNameList(FileListCount, 2)
Cells(FileListCount, "B").Value = FileNameList(FileListCount, 1)
Next File
End If
SortArray (FileNameList)
End Sub
Function SortArray(myArray)
' Tom Ogilvy bubble sort (modified)
' This sort simply takes a 2 dimentional and sorts the value install in
the second element
Dim temp, i, j, k
For i = LBound(myArray, 1) To UBound(myArray, 1) - 1
For j = i + 1 To UBound(myArray, 1)
If myArray(i, 2) > myArray(j, 2) Then
For k = LBound(myArray, 2) To UBound(myArray, 2)
temp = myArray(i, k)
myArray(i, k) = myArray(j, k)
myArray(j, k) = temp
Next
End If
Next
Next
SortArray = myArray
End Function
This is the ouput which suggests that no sort happened at all???
2008 05 02 13:48 file1
2008 05 06 15:08 file2
2008 05 06 15:09 file3
2008 05 06 15:25 file4
2008 05 02 13:45 file5
2008 04 30 08:27 file6
2008 04 30 09:10 file7
2008 05 01 13:00 file8
2008 05 02 13:44 file9
2008 05 02 13:38 file10
2008 05 02 13:40 file11
2008 04 30 08:13 file12
2008 05 02 13:48 file1
2008 05 06 15:08 file2
2008 05 06 15:09 file3
2008 05 06 15:25 file4
2008 05 02 13:45 file5
2008 04 30 08:27 file6
2008 04 30 09:10 file7
2008 05 01 13:00 file8
2008 05 02 13:44 file9
2008 05 02 13:38 file10
2008 05 02 13:40 file11
2008 04 30 08:13 file12
Any ideas?