Solved

vb.net - sub directories issue

Posted on 2011-09-21
5
264 Views
Last Modified: 2012-05-12
hello there,
I have about 3500 files, all in sub directories.
I am trying to get the full path for each of the files but this code only shows about 235 files and it wont show the full path.
how can I get the full path for all 3,500 files?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        lvCount.Text = 0
        If txtPath.Text <> "" Then
            Call leGetFiles(txtPath.Text)
        Else
            Debug.Print("You need to specify a directory")
        End If
    End Sub

    Private Sub leGetFiles(ByVal strDir As String)
        Dim qFiles = IO.Directory.GetDirectories(strDir)
        Dim xFiles = IO.Directory.GetFiles(strDir)
        For Each strFile In xFiles
            Debug.Print(strFile)
            lvCount.Text = Val(lvCount.Text) + 1
        Next
        Dim qDirs = IO.Directory.GetDirectories(strDir)
        For Each y In qDirs
            Dim subFiles = IO.Directory.GetFiles(IO.Path.Combine(strDir, y))
            For Each strFile In qFiles
                Debug.Print(strFile)
                lvCount.Text = Val(lvCount.Text) + 1
            Next
        Next
    End Sub

Open in new window

0
Comment
Question by:XK8ER
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 36577380
you will get the full paths for the Debug.Print with this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        lvCount.Text = 0
        If txtPath.Text <> "" Then
            Call leGetFiles(txtPath.Text)
        Else
            Debug.Print("You need to specify a directory")
        End If
    End Sub

    Private Sub leGetFiles(ByVal strDir As String)
        Dim qFiles = IO.Directory.GetDirectories(strDir)
        Dim xFiles = IO.Directory.GetFiles(strDir)
        For Each strFile In xFiles
            Debug.Print(Path.Combine(strDir, strFile))
            lvCount.Text = Val(lvCount.Text) + 1
        Next
        Dim qDirs = IO.Directory.GetDirectories(strDir)
        For Each y In qDirs
            Dim subFiles = IO.Directory.GetFiles(IO.Path.Combine(strDir, y))
            For Each strFile In qFiles
                Debug.Print(Path.Combine(IO.Path.Combine(strDir, y), strFile))
                lvCount.Text = Val(lvCount.Text) + 1
            Next
        Next
    End Sub

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
ID: 36577398
its not working because its showing 235 files only
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36577422
Try EnumerateFiles instead of GetFiles. According to the documentation, the only difference between them is that EnumerateFiles is more efficient when you have a lot of files and directories, but I have found out that they sometimes behave differently. For instance, WnumerateFiles is less prone to be stopped by an UnauthorizedAccessException.

Maybe it could solve you problem.
For Each file As String In IO.Directory.EnumerateFiles(strDir, "*.*", SearchOption.AllDirectories)
    Debug.WriteLine(file)
    lvCount.Text = Val(lvCount.Text) + 1
Next

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36577436
you must have sub directories within the sub directories. If this is the case and you want to include these as well, what is the depth of your search? i.e how deep is the directory structure?
0
 
LVL 1

Author Comment

by:XK8ER
ID: 36577440
about 8 sud dirs
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question