Looping through directories

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

    #1

    Looping through directories

    I am trying to loop through 2 separate directories (using the Dir) at the
    same time but it seems to get confused and is mixing the files from both
    directories. I tried to use the My.Computer.Get files method but I can't get
    it to work other than in the myDocuments directory. It also returns the full
    path when I only wnat the file name.
  • gene kelley

    #2
    Re: Looping through directories

    On Sun, 2 Jul 2006 15:12:01 -0700, Mark
    <Mark@discussio ns.microsoft.co mwrote:
    >I am trying to loop through 2 separate directories (using the Dir) at the
    >same time but it seems to get confused and is mixing the files from both
    >directories. I tried to use the My.Computer.Get files method but I can't get
    >it to work other than in the myDocuments directory. It also returns the full
    >path when I only wnat the file name.
    This example gets all the files in each of two directories and
    populates two list boxes with the respective filenames:

    Imports System.IO

    Private Sub Button1_Click(B yVal sender As System.Object, _
    ByVal e As System.EventArg s) Handles Button1.Click
    Dim dir1 As New DirectoryInfo(" C:\Windows")
    Dim FoundFiles1 As FileInfo() = dir1.GetFiles()
    Dim fiTemp As FileInfo
    For Each fiTemp In FoundFiles1
    Me.ListBox1.Ite ms.Add(fiTemp)
    Next fiTemp

    Dim dir2 As New DirectoryInfo(" C:\Windows\Syst em32")
    Dim FoundFiles2 As FileInfo() = dir2.GetFiles()
    For Each fiTemp In FoundFiles2
    Me.ListBox2.Ite ms.Add(fiTemp)
    Next fiTemp


    End Sub

    Gene

    Comment

    • Phill W.

      #3
      Re: Looping through directories

      Mark wrote:
      I am trying to loop through 2 separate directories (using the Dir) at the
      same time but it seems to get confused and is mixing the files from both
      directories.
      Dir uses an internal "pointer" to keep track of where it is within the
      current directory. You /can't/ "scan" two directories (or
      sub-directories) at the same time.

      Read up on the Directory class, particularly the GetFiles method(s).

      For Each sFile As String _
      In Directory.GetFi les( "path", "*.*" )
      Debug.WriteLine ( sFile )
      Next

      HTH,
      Phill W.

      Comment

      Working...