Get last 7days files from a folder

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
i need to get only last 7days files from a folder. using vb.net.

in my C drive i have a folder called LogFiles, and inside that logfiles folder i have more than 15-25 log files were there. and the name of each file will be the date in which it created.

eg: 20070607.log (this file created today).
eg: 20070503.log (created last month 3rd).

so i need to get the last 7 days log file by checking with current date.

how can i find the last 7days files from this folder. if you have any idea how to do this please let me know. if example is there, it will be very helpful to me.

thanks in advance.
 
its working by using this code.
VB.NET:
Dim FileDate As DateTime
Dim intdiff As Integer
Dim i As Integer
Dim di As New IO.DirectoryInfo("C:\LogFiles")
Dim aryFi As IO.FileInfo() = di.GetFiles("*.log")
Dim aFile As new ArrayList()
Dim fi As IO.FileInfo

For Each fi In aryFi
  Dim gname, gname1 As String
  gname = fi.Name
  gname1 = gname.Substring(0, 8)
  FileDate = System.DateTime.ParseExact(gname1, "yyyyMMdd",   System.Globalization.DateTimeFormatInfo.InvariantInfo).ToString("MM/dd/yyyy")
  intdiff = DateDiff(DateInterval.Day, FileDate, Now)
  If intdiff < 7 Then
    aFile.Add(gname)
  End If
Next
For i = 0 To aFile.Count - 1
  Dim name As String
  name = aFile(i).ToString()
  MessageBox.Show(name)
Next
 
Last edited by a moderator:
Back
Top