Solved

vb.net - sub directories issue

Posted on 2011-09-21
5
256 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now