Hi,
Are there any FSObject users that can spare a couple of minutes to help answer the folloing Q.
How would I use the FSObject to sort a number of files in a particular dir, in Date order???.
Any advice will be great.
Printable View
Hi,
Are there any FSObject users that can spare a couple of minutes to help answer the folloing Q.
How would I use the FSObject to sort a number of files in a particular dir, in Date order???.
Any advice will be great.
I have not tried this...
The first thing that pops up in my head as I read this
was
1. create a list view of these files
2. Display the file and the date as columns
3. Then use the listview sort to sort by the date column
Could I possibly trouble you to go inyo a bit more detail????
I am a newbie at all this.
just got this...
give me a few .... minutes
Any specific part you want to elaborate on?
1. Get a listView on your form
2. Right-Click on it
3. Go to Column Headers and Insert 2 columns
One might be called File, the other LastModifiedDate
Assume your list is called ListView1, put this code in the
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
with With ListView1
.SortKey = ColumnHeader.Index - 1
If .SortOrder = lvwAscending Then
.SortOrder = lvwDescending
Else
.SortOrder = lvwAscending
End If
.Sorted = True
End With
That would allow you to sort when you clik either of these
columns when you click on the column header
Now... populate the list view with the files you want to sort... (I don't know whether u did that already or not)
and the functionality is there
What I don't know...
Do you want to do this like I described here
or totally in the background where you would get rid
of all the files in a folder except the 5 most recent
or...
Depending on your answer, there may be more efficient ways to do this...
a little clarification could help immensely
I want to run this in the background and call it into other Subs i have.
I sort of understand the posting you made before, but I want to run this without any GUI for this command.
A solution without FSO, use dir function to retrieve the files in a directory and sort the files with sort shell :)
Code:Sub EnumDir(ByRef edir() As String, path As String)
Dim n As Integer, a As String
a = Dir(path)
Do While Len(a)
ReDim Preserve edir(n)
edir(n) = a
n = n + 1
a = Dir
Loop
End Sub
Sub Sort_shell_Filesbydate(a() As String)
Dim n&, i&, j&, k&, h
n = UBound(a)
k = n \ 2
While k > 0
For i = 0 To n - k
j = i
While (j >= 0) And DateDiff("s", FileDateTime(a(j)), FileDateTime(a(j + k))) > 0
h = a(j)
a(j) = a(j + k)
a(j + k) = h
If j > k Then
j = j - k
Else
j = 0
End If
Wend
Next i
k = k \ 2
Wend
End Sub
Sub Main()
Dim Files() As String, n As Long
EnumDir Files, "C:\"
ChDir ("C:\")
Sort_shell_Filesbydate Files
For n = 0 To UBound(Files)
Debug.Print Files(n)
Next n
End Sub