|
-
Oct 2nd, 2000, 06:22 AM
#1
Thread Starter
Addicted Member
Hi,
I am trying to sort files in a dir. I need to keep the 5 newest files and kill the others. Does anyone have any advice on the algo in VB that I can use or find.
Have you done a similar sort before.
Any Advice / help will be great.
Thank you in advance.
-
Oct 3rd, 2000, 03:38 PM
#2
_______
<?>
[code]
'bas module code
Option Explicit
Public Const MaxNumFiles = 5 ' Num of Files you want to keep
Public Type FileInfoType
FileName As String
FileDate As Date
End Type
Public Function GetRecentFileList(ByVal Path As String) As FileInfoType()
Dim filelist(1 To MaxNumFiles) As FileInfoType
Dim CurFile As String
Dim CurFileDate As Date
Dim IdxFile As Integer
Dim IdxMove As Integer
'this reflects a list1 on form1 to visually display 5 remaining file
'names and dates...Header Line and space 'not necessary to this code
Form1.List1.AddItem "These are the 5 files remaining in " & Path
Form1.List1.AddItem ""
'if the path is missing a \ then add it to the path
If Right(Path, 1) <> "\" Then Path = Path & "\"
'get the current file
CurFile = Dir(Path & "*.*")
'loop
Do While Len(CurFile)
'get the curfiledate for the current path and file
CurFileDate = FileDateTime(Path & CurFile)
'generate an index number
IdxFile = 1
'do while the current selection is > curfiledate
'example: if first file date is Oct 10,2000 then find the
'next file in the folder that is newer and add it to the
'array filelist()
Do While (filelist(IdxFile).FileDate > CurFileDate)
IdxFile = IdxFile + 1
If IdxFile = MaxNumFiles + 1 Then Exit Do
Loop
'if the index for looping =< 5 'maxnumfiles then reindex till you hit 5
If IdxFile <= MaxNumFiles Then
'starting at 6 step back by one and reindex the filelist() array
'so the actual 1st step will be 5 (maxnum)
For IdxMove = MaxNumFiles To IdxFile + 1 Step -1
filelist(IdxMove) = filelist(IdxMove - 1)
Next IdxMove
'obtain the filename and filedate
With filelist(IdxFile)
.FileName = CurFile
.FileDate = CurFileDate
End With
End If
'visual of what is happening and the 5 remaining files
'again not necessay..just a visual
Form1.List1.AddItem filelist(IdxFile).FileName & " / " & filelist(IdxFile).FileDate
CurFile = Dir()
Loop
GetRecentFileList = filelist()
End Function
Public Sub KillFilesNotInList(ByVal Path As String, filelist() As FileInfoType)
Dim CurFile As String
Dim IdxFile As Integer
Dim IsRecent As Boolean
'if the path is missing a \ then add it to the path
If Right(Path, 1) <> "\" Then Path = Path & "\"
CurFile = Dir(Path & "*.*")
'loop till no files left in dir
Do While Len(CurFile)
IsRecent = False 'set value to false
'loop through the array filelist() and then compare each file that file
'if the name matches one of the names in the filelist(0 array then
'exit the for IdxFile loop and get the next file and do it all again.
'If it doesn't match it is not among the 5 most recent so kill it
For IdxFile = 1 To UBound(filelist)
IsRecent = (StrComp(CurFile, filelist(IdxFile).FileName, vbTextCompare) = 0)
If IsRecent Then Exit For
Next IdxFile
'kill file
If Not IsRecent Then Kill Path & CurFile
CurFile = Dir()
Loop
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'form code
Option Explicit
Private Sub Command1_Click()
'declare an array of FileInfoType to hold the filelisting
Dim sFileList() As FileInfoType
sFileList = GetRecentFileList("c:\myfolder")
'kill all files in list except the 5 most recent by virute of create time
Call KillFilesNotInList("c:\myfolder", sFileList)
End Sub
[code]
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|