Link to home
Start Free TrialLog in
Avatar of bbimis
bbimis

asked on

need help finding the oldest directory that isn't empty using vb.net

can't seem to figure out how to get the oldest directory that isn't empty. i have tried the following code but end up getting the wrong directory
Private Function checkdiff()
        Try
            Dim folder As New DirectoryInfo("c:\program files")


            Dim oldest As DirectoryInfo = Nothing

            For Each dir As DirectoryInfo In folder.GetDirectories()
                If oldest Is Nothing Then
                    oldest = dir

                ElseIf dir.CreationTime < oldest.CreationTime Then

                    If dir.GetFiles.Length > 1 Then
                        oldest = dir
                    End If
                End If

            Next
            Dim difference As Long
            If oldest IsNot Nothing Then
                difference = DateDiff(DateInterval.Day, Date.Now, oldest.LastWriteTime)

                MsgBox("The difference in the date is: " & difference.ToString() & " Directory is : " & oldest.FullName)
            Else
                MsgBox("The directory does not exist")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbimis
bbimis

ASKER

That worked great. Didn't know I could do it that way. Thanks again.
Glad I could help :)