By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,787 Members | 2,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,787 IT Pros & Developers. It's quick & easy.

Retrieving "Next" file in a folder

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"johnb41" <or****@informatik.com> schrieb
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

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

P: n/a
On 19 May 2005 09:38:58 -0700, johnb41 wrote:
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
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.