Home All Groups Group Topic Archive Search About

Looping through directories

Author
2 Jul 2006 10:12 PM
Mark
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.

Author
3 Jul 2006 9:06 AM
gene kelley
On Sun, 2 Jul 2006 15:12:01 -0700, Mark
<M***@discussions.microsoft.com> 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. 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(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) 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.Items.Add(fiTemp)
        Next fiTemp

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


    End Sub

Gene
Author
4 Jul 2006 12:57 PM
Phill W.
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.GetFiles( "path", "*.*" )
     Debug.WriteLine( sFile )
Next

HTH,
    Phill  W.