Getting a list of all files created/modified in last x hours

  • Thread starter Thread starter wingers
  • Start date Start date
W

wingers

Guest
Hi

I am trying to find a way to get a list of all files created/modified in the last x hours.

This is something I can do easily from Powershell using

Get-ChildItem -Recurse | where-object { $_.lastwritetime -gt (get-date).addHours(-24)}


But I am trying to do it in VB and have tried using things such as Directory.GetFiles and Directory.EnumerateFiles e.g.

Dim today As DateTime = DateTime.Now.Date
Dim todaysFiles() As FileInfo = (New DirectoryInfo("c:\")).EnumerateFiles("*.*", searchOption.AllDirectories).Select(Function(x)
x.Refresh()
Return x
End Function).Where(Function(x) x.CreationTime.Date = today OrElse x.LastWriteTime = today).ToArray()



or

Dim directory = New DirectoryInfo("C:\")

Dim filesLst = directory.GetFiles("*.*", searchOption.AllDirectories).AsEnumerable().Where(Function(file) File.CreationTime.Date = DateTime.Now.Date).ToArray()


But these both through errors (as I would expect) when hitting folders such as System Volume Information or C:\$Recycle.Bin


Any ideas on how I can do what I need and just skip anything it doesn't have permission to access?


For info the app is running with admin rights






Darren Rose

Continue reading...
 
Back
Top