Results 1 to 10 of 10

Thread: [RESOLVED] Get the last file of a specific directory

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    Resolved [RESOLVED] Get the last file of a specific directory

    is there anyone who knows how i can get the last file in a specific drive path? e.g.
    D:\sample and the contents of this folders are :

    D:\sample\SIJFL2008081400
    D:\sample\SIJFL2008081401
    D:\sample\SIJFL2008081402

    how can i get D:\sample\SIJFL2008081402 using vb.net

    Thanks!

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get the last file of a specific directory

    So when you say last file, you mean the last one if they are sorted in alphabetical order yeah?

    You can use the My.Computer.FileSystem.GetFiles method to retrieve a list of all files in a directoy. Play around with that and if your still stuck (after you have actually tried to resolve the problem yourself) post back here and we will help you further.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the last file of a specific directory

    Create a DirectoryInfo object, call its GetFiles method to get an array of FileInfo objects, sort that array by file name and then get the last element (or sort in descending order and get the first element).

    I've posted examples of sorting FileInfo arrays before so a forum search will turn them up.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get the last file of a specific directory

    Doesnt the My.Computer.FileSystem.GetFiles method do the same thing as creating a directoryinfo object and then calling its GetFiles method? Is there an advantage of doing it the way you mentioned?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    Re: Get the last file of a specific directory

    hi again i've tried using Directory.GetFiles but it will return all the files in that specified path. all i want is the last file

    D:\sample\SIJFL2008081402

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get the last file of a specific directory

    Then you have not read what jmc said and understood it... You use the GetFiles method to return a list of the files THEN sort that list and get the last entry.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Junior Member
    Join Date
    Jun 2008
    Posts
    16

    Re: Get the last file of a specific directory

    try this one..

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sDir As String = "C:\files"
    Dim DirPath As System.io.Directory

    Dim FoundFiles() As String = DirPath.GetFiles(sDir, "*.txt")
    FoundFiles.Sort(FoundFiles)
    TextBox1.Text = FoundFiles(FoundFiles.Length - 1)
    End Sub

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get the last file of a specific directory

    Quote Originally Posted by smyle22
    try this one..

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sDir As String = "C:\files"
    Dim DirPath As System.io.Directory

    Dim FoundFiles() As String = DirPath.GetFiles(sDir, "*.txt")
    FoundFiles.Sort(FoundFiles)
    TextBox1.Text = FoundFiles(FoundFiles.Length - 1)
    End Sub
    Your using the IO.Directory class in the wrong way really. It should be like this:
    vb Code:
    1. Dim sDir As String = "D:\sample\"
    2. Dim FoundFiles() As String = IO.Directory.GetFiles(sDir)
    3. Array.Sort(FoundFiles)
    4. MessageBox.Show(FoundFiles(FoundFiles.Length - 1))
    Notice I also changed FoundFiles.Sort to Array.Sort

    Intellisense should warn you about all of this though by highlighting the line green
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get the last file of a specific directory

    I suggested using DirectoryInfo rather than Directory because you could sort the FileInfo objects by their Name property, which contains the file name only rather than the full path. Given that the folder path will be the same for every file though, that's not important so using Directory is the better option because it's simpler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    68

    Re: Get the last file of a specific directory

    Thanks very much guys for your help..the last 3 posts was very useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width