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:

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
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:

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
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?
 

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
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:

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
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?
 

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
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:

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
Tried that Paul, i got the error-

Type 'directoryinfo' is not defined.

?

Any ideas?
Thanks alot for your help by the way.
 

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
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
 

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
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?
 

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
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:

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
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...
 

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
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:

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
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?
 

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
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:

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
Hey paul, yeah will definitely rate.

Would that code also ignore a folder named "template issues" for example?
 

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
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.
 
Top Bottom