Retrieving "Next" file in a folder

  • Thread starter Thread starter johnb41
  • Start date Start date
J

johnb41

In my application, the user opens up a Tiff image. (The file does NOT
necessarily have to be the first file in the folder; it could by
anywhere.) When i'm done viewing (doing whatever) with the image I
need to open up the "Next" Tiff file in the folder. This should be
done by clicking a button instead of using the FileOpen dialog again
and again.

Here is my approach, which isn't working:

Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BtnConfirmNext.Click

Dim fi As New FileInfo("c:\test.tif")
Dim di As DirectoryInfo = fi.Directory
Dim NextImage As IEnumerator
NextImage = di.GetFiles.GetEnumerator
NextImage.MoveNext()
Dim fi2 As FileInfo = NextImage.Current

MessageBox.Show(fi2.Name)
'I was hoping fi2.name would yield the "NEXT" filename
'in the folder. Instead it gives me "c:\test.tif"
End Sub

My approach for this is probably way off. Can anyone help?

Thanks!
John
 
johnb41 said:
In my application, the user opens up a Tiff image. (The file does
NOT necessarily have to be the first file in the folder; it could
by anywhere.) When i'm done viewing (doing whatever) with the image
I need to open up the "Next" Tiff file in the folder. This should
be done by clicking a button instead of using the FileOpen dialog
again and again.

Here is my approach, which isn't working:

Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles BtnConfirmNext.Click

Dim fi As New FileInfo("c:\test.tif")
Dim di As DirectoryInfo = fi.Directory
Dim NextImage As IEnumerator
NextImage = di.GetFiles.GetEnumerator
NextImage.MoveNext()
Dim fi2 As FileInfo = NextImage.Current

MessageBox.Show(fi2.Name)
'I was hoping fi2.name would yield the "NEXT" filename
'in the folder. Instead it gives me "c:\test.tif"
End Sub

My approach for this is probably way off. Can anyone help?


The enumerator starts with the first file in the directory. Files in a
directory don't have a specific order, but you could first enumerate all the
files in the directory til test.tif is reached, then MoveNext to the next
file.

Armin
 
John,

The Dir function allows you to browse through the files in a
Folder/Directory. Here is some code that I wrote up. I haven't tested it so I
won't be surprised if it doesn't run the 1st time. However you should get the
idea from the code

///
Private Sub IterateFolder()
Dim File As String
Dim Path as String

Path = "c:\"
File = Dir$(Path & "*.tiff")

While File <> ""
MsgBox(File)
File = Dir()
End While

End Sub
\\\

HTH
 
Thanks Armin and Sarika,

Armin:
I tried what you suggested, but just couldn't find a way to get the
"next" file.

Sarika:
Interesting idea, thanks. Before I read your suggestion, I found my
own solution:

- First I iterated through the directoryinfo.filenames, retrieved the
file names, and put them in an arraylist object.
- Then I did arraylist.indexof(currentfile) to find out the index
position of the current file.
- Add 1 to it, and then that's your next file.
- Probably too much code for this, but it works.

Thanks again!
John
 
Thanks Armin and Sarika,

Armin:
I tried what you suggested, but just couldn't find a way to get the
"next" file.

Sarika:
Interesting idea, thanks. Before I read your suggestion, I found my
own solution:

- First I iterated through the directoryinfo.filenames, retrieved the
file names, and put them in an arraylist object.
- Then I did arraylist.indexof(currentfile) to find out the index
position of the current file.
- Add 1 to it, and then that's your next file.
- Probably too much code for this, but it works.

Thanks again!
John

Another approach: same idea, somewhat less code:

Dim files as ArrayList
dim n as Integer
Dim nextfile as String
files = new ArrayList(System.IO.Directory.GetFiles("C:\"))
n = files.BinarySearch("C:\test.tif")
If n < 0 then
' C:\test.tif wasn't found - return first filename
nextfile = files(0)
Elseif n >= files.Count Then
' C:\test.tif is last file - return empty string
nextfile = ""
Else
' return next file
nextfile = files(n+1)
End If
 
Back
Top