Is there any code to use in a VB app that can search
files by date? And display files accessed in the past 5 days?
Thanks,
Printable View
Is there any code to use in a VB app that can search
files by date? And display files accessed in the past 5 days?
Thanks,
use this code to delete the old files from ur pc
of course u must remove the kill statement & put ur codeCode:Dim MyFile, MyStamp, DestDrive, DestPath
MyFile = Dir(DestDrive + DestPath + "\*.txt")'Change Extension
Do
MyStamp = FileDateTime(DestDrive + DestPath + "\" + MyFile) MyStamp = DateValue(Left$(MyStamp, 10))
If MyStamp < PurgeResp Then Kill(DestDrive + DestPath + "\" + MyFile)
MyFile = Dir
If MyFile = "" Then Exit Do
Loop
MsgBox "Selected files purged.", vbOKOnly, "Purge Files"
Did you come up with a solution for returning a list of files that were last updated in a specified date range? I am looking to do the same thing and would like to know if there are API's that could retrieve this information
Code:'********************** bas module code
'for this example you need 1 listbox List1 and 1 command button Command1
'Get files in folder as per a date you supply
Option Explicit
Public myFileList()
Public Type FileInfoType
FileName As String
FileDate As Date
End Type
Public Function GetRecentFileList(ByVal Path As String, sDays As Integer) As FileInfoType()
Dim sList() As FileInfoType
Dim CurFile As String
Dim CurFileDate As Date
Dim IdxFile As Integer
Dim sNow As Date
'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 CurFile <> ""
CurFileDate = FileDateTime(Path & CurFile) 'file date
sNow = Now 'today's date
If DateDiff("d", CurFileDate, sNow) <= sDays Then
ReDim Preserve sList(IdxFile) As FileInfoType 'based on filecount of items in criteria
With sList(IdxFile) 'gather the date and time information
.FileName = CurFile
.FileDate = CurFileDate
End With
' Form1.List1.AddItem CurFile & sList(IdxFile).FileDate & _
" FileTime: " & sList(IdxFile).Filetime
ReDim Preserve myFileList(IdxFile) 'store the files and dates and times in an array
myFileList(IdxFile) = "File Name: " & sList(IdxFile).FileName _
& " FileTime: " & sList(IdxFile).FileDate
IdxFile = IdxFile + 1 'increment the counter
End If
CurFile = Dir() 'next file
Loop
GetRecentFileList = sList()
End Function
'********************** Form Code
'change C:\my documents for your directory
'change 5 to reflect the number of days you need
Option Explicit
Private Sub Command1_Click()
'declare an array of FileInfoType to hold the filelisting
Dim sFileList() As FileInfoType, x As Integer
sFileList = GetRecentFileList("c:\my documents", 5) 'get files from last 5 days
For x = LBound(myFileList) To UBound(myFileList)
List1.AddItem myFileList(x)
Next x
End Sub