473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Search for files in folders?

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
Sep 7 '06 #1
6 1388
"Joris De Groote" <jo************@skynet.bewrote 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.
Sep 7 '06 #2
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?...6-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
"Joris De Groote" <jo************@skynet.beschreef in bericht
news:uV**************@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


Sep 7 '06 #3

"Joris De Groote" <jo************@skynet.bewrote in message
news:uV**************@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
Sep 7 '06 #4
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

Sep 7 '06 #5
"Joris De Groote" <jo************@skynet.beschrieb:
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/>

Sep 7 '06 #6
I'm going to reply like this, so I can have every one in one post :).
Thanks for the help!

Joris

"Joris De Groote" <jo************@skynet.beschreef in bericht
news:uV**************@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


Sep 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
0
by: mm | last post by:
Can someone with Win 2K please help me here. Best yet, can some MVP duplicate it and report it to MS. I can't find any reference to this issue on the Web, or in MS Knowledge Base. I have been...
5
by: tmb | last post by:
I need to search a folder & sub-folders for key words in ASP files... I can open the files with Notepad and see the text string there... But when I try to navigate to the folder with Windows...
1
by: Marco | last post by:
Hi all, Could anyone please help me? I need to do a c# application to search a word or phrase in the name of files and folders, AND INSIDE documents in word, excel and pdf formats. Could...
1
by: bhowerton | last post by:
Hello, I am new to programming in Python, but I am very excited about the possibilities that it (Python) has. I maybe jumping the gun a little bit here, but this is what I would like to do:...
7
by: molle | last post by:
Hi! Does anyone know a searchscript to create a javascript file-search. I want it to be able to search for a specific filename in a folder INCLUDING its sub-folders. I know there is a way to...
9
by: Lloyd Sheen | last post by:
For all those who don't think that a recursive search of files in folders is a good thing in the Microsoft.VisualBasic.FileIO.FileSystem namespace listen to this. I am reorg my mp3 collection. ...
3
by: =?Utf-8?B?Tm9ybUQ=?= | last post by:
It isn't clear this is the right place to post this, but it IS related to ..NET CONFIG files... Windows Explorer (at least on W2003Server) doesn't seem to know that .config files exist. ...
3
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.