Retrieving "Next" file in a folder

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnb41

    Retrieving "Next" file in a folder

    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles BtnConfirmNext. Click

    Dim fi As New FileInfo("c:\te st.tif")
    Dim di As DirectoryInfo = fi.Directory
    Dim NextImage As IEnumerator
    NextImage = di.GetFiles.Get Enumerator
    NextImage.MoveN ext()
    Dim fi2 As FileInfo = NextImage.Curre nt

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

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

    Thanks!
    John

  • Armin Zingler

    #2
    Re: Retrieving "Next&quot ; file in a folder

    "johnb41" <orders@informa tik.com> schrieb[color=blue]
    > 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(B yVal sender As System.Object, ByVal e
    > As
    > System.EventArg s) Handles BtnConfirmNext. Click
    >
    > Dim fi As New FileInfo("c:\te st.tif")
    > Dim di As DirectoryInfo = fi.Directory
    > Dim NextImage As IEnumerator
    > NextImage = di.GetFiles.Get Enumerator
    > NextImage.MoveN ext()
    > Dim fi2 As FileInfo = NextImage.Curre nt
    >
    > MessageBox.Show (fi2.Name)
    > 'I was hoping fi2.name would yield the "NEXT" filename
    > 'in the folder. Instead it gives me "c:\test.ti f"
    > End Sub
    >
    > My approach for this is probably way off. Can anyone help?[/color]


    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

    Comment

    • Sarika

      #3
      Re: Retrieving &quot;Next&quot ; file in a folder

      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

      Comment

      • johnb41

        #4
        Re: Retrieving &quot;Next&quot ; file in a folder

        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.f ilenames, retrieved the
        file names, and put them in an arraylist object.
        - Then I did arraylist.index of(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

        Comment

        • Ross Presser

          #5
          Re: Retrieving &quot;Next&quot ; file in a folder

          On 19 May 2005 09:38:58 -0700, johnb41 wrote:
          [color=blue]
          > 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.f ilenames, retrieved the
          > file names, and put them in an arraylist object.
          > - Then I did arraylist.index of(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[/color]

          Another approach: same idea, somewhat less code:

          Dim files as ArrayList
          dim n as Integer
          Dim nextfile as String
          files = new ArrayList(Syste m.IO.Directory. GetFiles("C:\") )
          n = files.BinarySea rch("C:\test.ti f")
          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

          Comment

          Working...