Friday, September 12, 2008

Drill down into directory - vb.net

'A recursive function that drills down through
'subfolders and checks file information


Public Sub GetFiles(ByVal DirName As String)
'pass in the directory name

Dim d As DirectoryInfo = New DirectoryInfo(DirName)
'Creating a DirectoryInfo object

Dim f() As FileInfo = d.GetFiles()
'Creating a FileInfo Array, since we
'will have multiple files
'Calling DirectoryInfos GetFiles method
'which returns the array of files
'in the directory


Dim fi As FileInfo
'Creating a single fileinfo object


For Each fi In f
'for each fileinfo object
'in the fileinfo array

If (fi.LastWriteTime.Date.ToString("MM-dd-yyyy") = Today.AddDays(-1).Date.ToString("MM-dd-yyyy")) Then

'Do what ever you want, in my case I'm checking for files modified yesterday

End If

Next



Dim innerFolders As DirectoryInfo
'work on the inner folders

For Each innerFolders In d.GetDirectories()
'call the function again with a new
'directory to check


GetFiles(innerFolders.FullName)

Next

End Sub

No comments: