Looping through directories

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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
 
Mark said:
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.
 
Back
Top