Home All Groups Group Topic Archive Search About

Search for files in folders?

Author
7 Sep 2006 4:29 PM
Joris De Groote
Hi,

I have a program that must look in in certain folder and take out every file
(also the files in subfolders). Now it's no problem to do this with a few
for's and if's. However, I don't know how many subfolders there will be, it
can be 1, it can be 3, but it could be 15 to. I don't want to write all
these if's and for's in each other and than still be limited with the
predefined number of folders that are coded in the for/if structure. So can
someone help me out here without having me to write that very long
structure?

Thanks
Joris


----example of code how I can write it----
'Inlezen aantal Sitemappen

dir1 = System.IO.Directory.GetDirectories(bronmap)

Dim i As Integer

If dir1.GetLength(0) > 0 Then

'Mappen gevonden in ZipBronMap

For i = 0 To (dir1.GetLength(0) - 1)

'Mappen in dir1

'Inlezen mappen

dir2 = System.IO.Directory.GetDirectories(dir1(i) + "\")

If dir2.GetLength(0) > 0 Then

.......... (more code here but can't copy all, it's like always the same...)

End If

Next

'Geen mappen gevonden in ZipBronMap (dir1 leeg)

'Kijken naar PDF Bestanden & Verwerken

files = System.IO.Directory.GetFiles(bronmap)

If files.GetLength(0) > 0 Then

Bestand_Controle(files)

End If

Else

'Geen mappen gevonden in ZipBronMap (dir1 leeg)

'Kijken naar PDF Bestanden & Verwerken

files = System.IO.Directory.GetFiles(bronmap)

If files.GetLength(0) > 0 Then

Bestand_Controle(files)

End If

End If

Author
7 Sep 2006 4:56 PM
Spam Catcher
"Joris De Groote" <joris.degro***@skynet.be> wrote in news:uV2zQrp0GHA.1268
@TK2MSFTNGP02.phx.gbl:

> So can
> someone help me out here without having me to write that very long
> structure?

Write a recursive function.
Author
7 Sep 2006 5:01 PM
Robinson
"Joris De Groote" <joris.degro***@skynet.be> wrote in message
news:uV2zQrp0GHA.1268@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> I have a program that must look in in certain folder and take out every
> file (also the files in subfolders). Now it's no problem to do this with a
> few for's and if's. However, I don't know how many subfolders there will
> be, it can be 1, it can be 3, but it could be 15 to. I don't want to write
> all these if's and for's in each other and than still be limited with the
> predefined number of folders that are coded in the for/if structure. So
> can someone help me out here without having me to write that very long
> structure?
>

You want to use Recursion, so that given a single high level folder
(Ancestor), the algorithm will iterate all lower level sub-folders
(Descendants).  Here's what it might look like:

Public Sub Iterate(ByVal theRoot As String)
    Iterate_Aux(theRoot)
End Sub

Public Sub Iterate_Aux(ByVal theRoot As String)

    ' Process the files in this directory

    Dim theFiles() As String = Directory.GetFiles(theRoot)

    For Each theFile As String In theFiles

        ' ... process the file in whatever way you want....

    Next



    ' Recurse to sub-directorys to process those files too...

    Dim theDirectories() As String = Directory.GetDirectories(theRoot)

    For Each theDirectory As String In theDirectories

        ' Recurse down a level on this branch...

        Iterate_Aux(theDirectory)

    Next

End Sub
Author
7 Sep 2006 5:03 PM
Cor Ligthert [MVP]
Joris,

You have to do that recursive.

I have today not much time, to see an example of recursive have a look at
this one, which is about controls.

http://www.vb-tips.com/dbpages.aspx?ID=8ff290d4-5c16-4cef-ba06-56e3599238c1

The way is that you call the method that is processing in the process.
Be sure that it is able to get to an end, but that is obvious in yours.

Cor


Show quoteHide quote
"Joris De Groote" <joris.degro***@skynet.be> schreef in bericht
news:uV2zQrp0GHA.1268@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> I have a program that must look in in certain folder and take out every
> file (also the files in subfolders). Now it's no problem to do this with a
> few for's and if's. However, I don't know how many subfolders there will
> be, it can be 1, it can be 3, but it could be 15 to. I don't want to write
> all these if's and for's in each other and than still be limited with the
> predefined number of folders that are coded in the for/if structure. So
> can someone help me out here without having me to write that very long
> structure?
>
> Thanks
> Joris
>
>
> ----example of code how I can write it----
> 'Inlezen aantal Sitemappen
>
> dir1 = System.IO.Directory.GetDirectories(bronmap)
>
> Dim i As Integer
>
> If dir1.GetLength(0) > 0 Then
>
> 'Mappen gevonden in ZipBronMap
>
> For i = 0 To (dir1.GetLength(0) - 1)
>
> 'Mappen in dir1
>
> 'Inlezen mappen
>
> dir2 = System.IO.Directory.GetDirectories(dir1(i) + "\")
>
> If dir2.GetLength(0) > 0 Then
>
> ......... (more code here but can't copy all, it's like always the
> same...)
>
> End If
>
> Next
>
> 'Geen mappen gevonden in ZipBronMap (dir1 leeg)
>
> 'Kijken naar PDF Bestanden & Verwerken
>
> files = System.IO.Directory.GetFiles(bronmap)
>
> If files.GetLength(0) > 0 Then
>
> Bestand_Controle(files)
>
> End If
>
> Else
>
> 'Geen mappen gevonden in ZipBronMap (dir1 leeg)
>
> 'Kijken naar PDF Bestanden & Verwerken
>
> files = System.IO.Directory.GetFiles(bronmap)
>
> If files.GetLength(0) > 0 Then
>
> Bestand_Controle(files)
>
> End If
>
> End If
>
>
Author
7 Sep 2006 8:45 PM
zacks
Imports System.IO

Public Class EnumDir

    Private _Root As String
    Private _Files As List(Of String)
    Private _Folders As List(Of String)
    Private _TotalSize As Long
    Private _TotalFiles As Long
    Private _TotalFolders As Long

    Public Property Root() As String
        Get
            Return _Root
        End Get
        Set(ByVal value As String)
            _Root = value
        End Set
    End Property

    Public Property Files() As List(Of String)
        Get
            Return _Files
        End Get
        Set(ByVal value As List(Of String))
            _Files = value
        End Set
    End Property

    Public Property Folders() As List(Of String)
        Get
            Return _Folders
        End Get
        Set(ByVal value As List(Of String))
            _Folders = value
        End Set
    End Property

    Public Property TotalSize() As Long
        Get
            Return _TotalSize
        End Get
        Set(ByVal value As Long)
            _TotalSize = value
        End Set
    End Property

    Public Property TotalFiles() As Long
        Get
            Return _TotalFiles
        End Get
        Set(ByVal value As Long)
            _TotalFiles = value
        End Set
    End Property

    Public Property TotalFolders() As Long
        Get
            Return _TotalFolders
        End Get
        Set(ByVal value As Long)
            _TotalFolders = value
        End Set
    End Property

    Public Sub New()

        _Root = ""
        _Files = New List(Of String)
        _Folders = New List(Of String)
        _TotalSize = 0
        _TotalFiles = 0
        _TotalFolders = 0

    End Sub

    ''' <summary>
    ''' Enumerates all Files in a Folder Tree
    ''' </summary>
    ''' <param name="Root"></param>
    ''' <remarks>Root Directory to Enumerate</remarks>
    Public Sub New(ByVal Root As String)

        _Root = Root
        _Files = New List(Of String)
        _Folders = New List(Of String)
        _TotalSize = 0
        _TotalFiles = 0
        _TotalFolders = 0
        Me.GetFiles(_Root)

    End Sub

    Public Sub GetFiles(ByVal Path As String)

        Dim myDirectoryRoot As New DirectoryInfo(Path)
        Dim di As DirectoryInfo
        Dim fi As FileInfo
        Dim lSize As Long = 0

        For Each fi In myDirectoryRoot.GetFiles
            _Files.Add(fi.FullName)
            _TotalFiles += 1
            _TotalSize += fi.Length
        Next
        For Each di In myDirectoryRoot.GetDirectories()
            _Folders.Add(di.FullName)
            _TotalFolders += 1
            GetFiles(di.FullName)
        Next
        myDirectoryRoot = Nothing

    End Sub

End Class
Author
7 Sep 2006 10:16 PM
Herfried K. Wagner [MVP]
"Joris De Groote" <joris.degro***@skynet.be> schrieb:
> I have a program that must look in in certain folder and take out every
> file (also the files in subfolders). Now it's no problem to do this with a
> few for's and if's. However, I don't know how many subfolders there will
> be, it can be 1, it can be 3, but it could be 15 to. I don't want to write
> all these if's and for's in each other and than still be limited with the
> predefined number of folders that are coded in the for/if structure. So
> can someone help me out here without having me to write that very long
> structure?

If you are looking for a complete multithreaded solution, check out the
following sample:

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/FileSystemEnumerator.zip>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
14 Sep 2006 9:05 PM
Joris De Groote
I'm going to reply like this, so I can have every one in one post :).
Thanks for the help!

Joris

Show quoteHide quote
"Joris De Groote" <joris.degro***@skynet.be> schreef in bericht
news:uV2zQrp0GHA.1268@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> I have a program that must look in in certain folder and take out every
> file (also the files in subfolders). Now it's no problem to do this with a
> few for's and if's. However, I don't know how many subfolders there will
> be, it can be 1, it can be 3, but it could be 15 to. I don't want to write
> all these if's and for's in each other and than still be limited with the
> predefined number of folders that are coded in the for/if structure. So
> can someone help me out here without having me to write that very long
> structure?
>
> Thanks
> Joris
>
>
> ----example of code how I can write it----
> 'Inlezen aantal Sitemappen
>
> dir1 = System.IO.Directory.GetDirectories(bronmap)
>
> Dim i As Integer
>
> If dir1.GetLength(0) > 0 Then
>
> 'Mappen gevonden in ZipBronMap
>
> For i = 0 To (dir1.GetLength(0) - 1)
>
> 'Mappen in dir1
>
> 'Inlezen mappen
>
> dir2 = System.IO.Directory.GetDirectories(dir1(i) + "\")
>
> If dir2.GetLength(0) > 0 Then
>
> ......... (more code here but can't copy all, it's like always the
> same...)
>
> End If
>
> Next
>
> 'Geen mappen gevonden in ZipBronMap (dir1 leeg)
>
> 'Kijken naar PDF Bestanden & Verwerken
>
> files = System.IO.Directory.GetFiles(bronmap)
>
> If files.GetLength(0) > 0 Then
>
> Bestand_Controle(files)
>
> End If
>
> Else
>
> 'Geen mappen gevonden in ZipBronMap (dir1 leeg)
>
> 'Kijken naar PDF Bestanden & Verwerken
>
> files = System.IO.Directory.GetFiles(bronmap)
>
> If files.GetLength(0) > 0 Then
>
> Bestand_Controle(files)
>
> End If
>
> End If
>
>