|
-
Aug 8th, 2008, 04:39 AM
#1
Thread Starter
Lively Member
[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!
-
Aug 8th, 2008, 04:49 AM
#2
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.
-
Aug 8th, 2008, 04:55 AM
#3
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.
-
Aug 8th, 2008, 05:00 AM
#4
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?
-
Aug 8th, 2008, 05:05 AM
#5
Thread Starter
Lively Member
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
-
Aug 8th, 2008, 05:19 AM
#6
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.
-
Aug 8th, 2008, 05:31 AM
#7
Junior Member
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
-
Aug 8th, 2008, 05:41 AM
#8
Re: Get the last file of a specific directory
 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:
Dim sDir As String = "D:\sample\"
Dim FoundFiles() As String = IO.Directory.GetFiles(sDir)
Array.Sort(FoundFiles)
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
-
Aug 8th, 2008, 09:25 AM
#9
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.
-
Aug 12th, 2008, 02:55 AM
#10
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|