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:
try this

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

Dim curMonth As String = Format(Now, "MMM yyyy")
Process.Start("\\IFADATAFLOW1\Avery LTD\" & curMonth & "\03/09/2007")

End Sub

for the latest subfolder look up Date.Compare()
 
Last edited by a moderator:
Thanks! That works perfectly...now I just need to get it to open the correct days date too.

I looked into using Date.Compare(), but I'm not completely sure how to use it, can you help at all?
 
VB.NET:
Dim curMonth As String = Format(Now, "MMM yyyy")
dim dir as new directoryinfo(("\\IFADATAFLOW1\Avery LTD\" & curMonth)

Dim subDirs() As String = dir.getdirectories
Dim lstSubDirs As New ArrayList(subDirs)
lstSubDirs.Sort()

Process.Start("\\IFADATAFLOW1\Avery LTD\" & curMonth & "\" & lstSubDirs(lstSubDirs.Count - 1))
 
Last edited by a moderator:
Thanks Paul that looks like what I need, there's one problem though, in this part -

VB.NET:
Dim subDirs() As String = Dir.getdirectories

I get the message 'getdirectories' is not a member of 'String'.

Do you know why that is?
 
i can't recreate your directory structure because it contains invalid characters but
try this:
VB.NET:
Dim curMonth As String = Format(Now, "MMM yyyy")
dim dir as new directoryinfo(("\\IFADATAFLOW1\Avery LTD\" & curMonth))

Dim x As Integer = 0
Dim subDirs(x) As String
Dim dirItem As DirectoryInfo

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

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

Process.Start("\\IFADATAFLOW1\Avery LTD\" & curMonth & "\" & lstSubDirs(lstSubDirs.Count - 1))
i just noticed. / is an invalid character but - isn't
should still work
 
Last edited:
Tried that Paul, i got the error-

Type 'directoryinfo' is not defined.

?

Any ideas?
Thanks alot for your help by the way.
 
Kind of, I did need to add an 'IO' in there, DirectoryInfo wasn't recognised so i changed it to IO.DirectoryInfo and that works perfectly! Thanks alot for your help, got there in the end.

Just in case anyone else ever needs to do this, ill post the working code I'm using -

VB.NET:
Public Class Form1

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


        Dim curMonth As String = Format(Now, "MMM yyyy")
        Dim dir As New IO.DirectoryInfo(("c:\Avery LTD\" & curMonth))

        Dim x As Integer = 0
        Dim subDirs(x) As String
        Dim dirItem As IO.DirectoryInfo


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

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

        Process.Start("c:\Avery LTD\" & curMonth & "\" & lstSubDirs(lstSubDirs.Count - 1))
    End Sub
End Class
 
Paul (or anyone), I've put this program into testing this morning (see code in my post above). I've got a slight problem with it though.

The code is set up to look for the main customer folder and then the sub folder named with the current month.

The problem with that is, when a new month begins, there isn't going to be a folder created for that month until the day when some work comes in for that customer, that could be the 3rd, or 4th.
So when the program looks for that month folder, and it isnt there, it crashes.

Would it be possible to make it look for the month folder in the same way that it opens the specific day folder? that way, if there was no 'oct 2007' folder, it would be opening the 'sep 2007' folder.

So in other words, can i make the month folder open based on the most recent creation date, in the same way as the day folder does?

I have tried this myself but it doesnt work...i tried this...

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 subDirs(x) As String
        Dim dirItem As IO.DirectoryInfo


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

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

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

I was really just trying to use the code in the same way as you did, but it doesnt work...can you give me a hand with this?
 
try it this way

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 subDirs(x) As String
Dim dirItem As IO.DirectoryInfo


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

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

dim latestMonth as string = lstSubDirs(lstSubDirs.Count - 1)
erase subDirs
lstSubDirs.Clear()

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

x = 0
ReDim Preserve subDirs(x)

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))
 
Last edited:
Thanks, I've been testing that out and the code seems to be sorting the month subfolders alphabetically rather than by date, so in practice, it navigates through the September folder every time...the subfolders for the specific days always opens correctly on the most recent creation date though.

I've tried a few things but haven't managed to get it working yet, let me know if you can think of anything...
 
i thought it might sort alphabetically
try this


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


        For Each dirItem In dir.GetDirectories
            subDirsM(x) = CType(dirItem.ToString, Date)
            x += 1
            ReDim Preserve subDirsM(x)
        Next

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

        Dim latestMonth As String = Format(lstSubDirs(lstSubDirs.Count - 1), "MMM yyyy")
        
        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))
 
Last edited:
Back
Top