opening folder based on creation date

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
Hi all

Ive created a simple program that opens up certain folders on my local and network drives by clicking on specific buttons, it acts as a navigation menu because i spend far too long clicking around to get to the right folders.

But the way it works isn't really good enough.

The way the folders are set up on my computer is like this...We have a main customer job folder, within this folder are several subfolders that are split down by month, and then within each of these folders are a list of subfolders split down by the days date.

So the folder structure looks like this -

-----Customer Name
-------------------May 2007
-------------------------------01/05/2007
-------------------------------02/05/2007
-------------------------------03/05/2007
-------------------June 2007
-------------------------------01/06/2007
-------------------------------02/06/2007
-------------------------------03/06/2007
-------------------Jul 2007
-------------------------------01/07/2007
-------------------------------02/07/2007
-------------------------------03/07/2007


The only folder I'm ever interested in opening is the most recent date, so if it was july the 3rd, I'd want to open "customer name/july 2007/03-07-2007".

However, the program I have created only opens up the main "customer folder" because im only using basic linking, and if i linked to a more specific date, I'd constantly have to change the code each day to open the new days date.
This is the code I'm using for each button-


VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Process.Start("\\IFADATAFLOW1\Avery LTD\Oct 2007\03/09/2007")

End Sub
It would be much better if i could get some code that would look within the "Customer Name" folder and then access the folder with the most recent creation date within it, this would take me to the folder for the current month, and then it would finally open the most recently created folder within that too, which would, as an end result, open up the folder for todays date, or at least the most recent date.

Can someone tell me how I would go about doing this?

I hope someone can help, this is something that has been bothering me for a while now!

I'm using visual basic express to create this program by the way.
 
Last edited by a moderator:
yep that works correctly now thanks alot for that!

One last question on this subject though - because of the strict date format applied in that code, the program will crash if there is a folder in the directory that doesnt apply to the rule 'MMM YYYY'. For example in some folders there is an old folder named '2006' which is just an archive of last years work.
Also because other people create these folders sometimes, they may name folders 'october 2007' instead of 'oct 2007' etc, which could occasionally cause an issue.

Is there a way of achieving the same result without specifying a folder name format?
 
try this. it'll ignore the archive directories + check for abbreviated or full month names

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim dir As New IO.DirectoryInfo(("c:\SharedDocs\Avery LTD"))

        Dim x As Integer = 0
        Dim subDirsM(x) As Date
        Dim dirItem As IO.DirectoryInfo

        On Error Resume Next

        For Each dirItem In dir.GetDirectories
            subDirsM(x) = CType(dirItem.ToString, Date)
            If Err.Number = 0 Then
                x += 1
                ReDim Preserve subDirsM(x)
            Else
                Err.Clear()
            End If
        Next

        Dim lstSubDirs As New ArrayList(subDirsM)
        lstSubDirs.Sort()

        Dim latestMonth As String = Format(lstSubDirs(lstSubDirs.Count - 1), "MMM yyyy")

        If Not IO.Directory.Exists("c:\SharedDocs\Avery LTD\" & latestMonth) Then
            latestMonth = Format(lstSubDirs(lstSubDirs.Count - 1), "MMMM yyyy")
        End If
        
        lstSubDirs.Clear()

        dir = New IO.DirectoryInfo(("c:\SharedDocs\Avery LTD\" & latestMonth))

        x = 0
        Dim subDirs(x) as string

        For Each dirItem In dir.GetDirectories
             subDirs(x) = dirItem.ToString
             x += 1
             ReDim Preserve subDirs(x)
        Next

        lstSubDirs = New ArrayList(subDirs)
        lstSubDirs.Sort()

        Process.Start("c:\SharedDocs\Avery LTD\" & latestMonth & "\" & lstSubDirs(lstSubDirs.Count - 1))

rate me if this helps
 
Last edited:
Hey paul, yeah will definitely rate.

Would that code also ignore a folder named "template issues" for example?
 
that works completely now, tested it thoroughly today, thanks alot.

I have another question for you if you dont mind...

ex.jpg



In that image there are two buttons, the one on the left opens a folder using that code you gave me...the button on the right displays the file contents of that folder in a listbox.

ive been using this code to do this...

VB.NET:
        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo("c:\temp")
        Dim diar1 As IO.FileInfo() = dir.GetFiles()
        Dim dra As IO.FileInfo

        'list the names of all files in the specified directory
        For Each dra In diar1
            ListBox1.Items.Add(dra)
        Next

This works fine for normal purposes but it only displays the contents of a fixed directory and obviously doesnt navigate to the latest date in the same way as the other code you did for me.

Is there a way to combine the two so that i can display the contents of the specific days folder in my listbox?

I thought it would be best to ask you about this one seeing as you understand what im trying to do.
 
i would recommend doing it this way

VB.NET:
Private function getLatestFolder as string
        
        Dim dir As New IO.DirectoryInfo(("c:\SharedDocs\Avery LTD"))

        Dim x As Integer = 0
        Dim subDirsM(x) As Date
        Dim dirItem As IO.DirectoryInfo

        On Error Resume Next

        For Each dirItem In dir.GetDirectories
            subDirsM(x) = CType(dirItem.ToString, Date)
            If Err.Number = 0 Then
                x += 1
                ReDim Preserve subDirsM(x)
            Else
                Err.Clear()
            End If
        Next

        Dim lstSubDirs As New ArrayList(subDirsM)
        lstSubDirs.Sort()

        Dim latestMonth As String = Format(lstSubDirs(lstSubDirs.Count - 1), "MMM yyyy")

        If Not IO.Directory.Exists("c:\SharedDocs\Avery LTD\" & latestMonth) Then
            latestMonth = Format(lstSubDirs(lstSubDirs.Count - 1), "MMMM yyyy")
        End If
        
        lstSubDirs.Clear()

        dir = New IO.DirectoryInfo(("c:\SharedDocs\Avery LTD\" & latestMonth))

        x = 0
        Dim subDirs(x) as string

        For Each dirItem In dir.GetDirectories
             subDirs(x) = dirItem.ToString
             x += 1
             ReDim Preserve subDirs(x)
        Next

        lstSubDirs = New ArrayList(subDirs)
        lstSubDirs.Sort()

        return latestMonth & "\" & lstSubDirs(lstSubDirs.Count - 1))

end function

then you can call it from your button_click events

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Process.Start("c:\SharedDocs\Avery LTD\" & getLatestFolder)
end sub

VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo("c:\SharedDocs\Avery LTD\" & getLatestFolder)
        Dim diar1 As IO.FileInfo() = dir.GetFiles()
        Dim dra As IO.FileInfo

        'list the names of all files in the specified directory
        For Each dra In diar1
            ListBox1.Items.Add(dra)
        Next
end sub
 
Thanks, just so i understand, the Private Function code should be placed outside of the button_click events, and then the other two bits are placed within their respective button_click events?

I have two questions though - I have lots of different buttons opening up different customer folders...

ex9.jpg


...so should i use that private function code before each specific button_click event, just changing the directory path each time? Because using that code, the directory path is being declared outside the button_click, so it wouldnt cater for each customer folder button?

Also, in the code ive quoted below (the listbox code), the "dir.getfiles" is creating an error saying - "'GetFiles' is not a member of 'String'", any idea whats causing that? that bit worked ok before.



VB.NET:
        Dim di As New IO.DirectoryInfo("\\IFADATAFLOW1\SharedDocs\Bankhall live work\" & getLatestFolder())
        Dim diar1 As IO.FileInfo() = [B]Dir.GetFiles[/B]()
        Dim dra As IO.FileInfo

        For Each dra In diar1
            ListBox1.Items.Add(dra)
        Next

hope you can help me with this!
 
the function should be placed as it is in your form code. the button click codes are as they should be too except

VB.NET:
Dim dir As New IO.DirectoryInfo("\\IFADATAFLOW1\SharedDocs\Bankhall live work\" & getLatestFolder())
ListBox1.Items.AddRange(Dir.GetFiles())

the function is of type string. you call it by name and it returns a string.
as far as the other buttons go, it depends what folders you want to open
 
if all of your customer folders have month folders containing daily folders you could modify the getLatestFolder function

VB.NET:
Private function getLatestFolder(startDir as string) as string
        
        Dim dir As New IO.DirectoryInfo(startDir)

        Dim x As Integer = 0
        Dim subDirsM(x) As Date
        Dim dirItem As IO.DirectoryInfo

        On Error Resume Next

        For Each dirItem In dir.GetDirectories
            subDirsM(x) = CType(dirItem.ToString, Date)
            If Err.Number = 0 Then
                x += 1
                ReDim Preserve subDirsM(x)
            Else
                Err.Clear()
            End If
        Next

        Dim lstSubDirs As New ArrayList(subDirsM)
        lstSubDirs.Sort()

        Dim latestMonth As String = Format(lstSubDirs(lstSubDirs.Count - 1), "MMM yyyy")

        If Not IO.Directory.Exists(startDir  & latestMonth) Then
            latestMonth = Format(lstSubDirs(lstSubDirs.Count - 1), "MMMM yyyy")
        End If
        
        lstSubDirs.Clear()

        dir = New IO.DirectoryInfo((startDir  & latestMonth))

        x = 0
        Dim subDirs(x) as string

        For Each dirItem In dir.GetDirectories
             subDirs(x) = dirItem.ToString
             x += 1
             ReDim Preserve subDirs(x)
        Next

        lstSubDirs = New ArrayList(subDirs)
        lstSubDirs.Sort()

        return latestMonth & "\" & lstSubDirs(lstSubDirs.Count - 1))

end function

then you can call it like this

VB.NET:
dim startDir as string = "\\IFADATAFLOW1\SharedDocs\Bankhall live work\"
Dim dir As New IO.DirectoryInfo(startDir & getLatestFolder(startDir))
ListBox1.Items.AddRange(Dir.GetFiles())
 
Paul, just tried that out, and it works very well, alot better than I'd imagined it would when i first asked about it. Thanks for that!
 
Back
Top