Processing files within folder recursively

rangerbud249

Active member
Joined
Aug 18, 2004
Messages
27
Programming Experience
Beginner
Im having trouble finding code that will help me go thru a list of folders to check if it contains certain files.

ex:
once I connect to the folder LinuxLogFiles I have hundreds of folders within that folder.

I need to go thru each of those folders and see if it contains a:

VSU.log, local1.log,

If it does I need it to also check if it contains a :
seen.txt, parsed.txt, or seen.txt and parsed.txt

If it does then I will perform different actions.

I just graduated from college and started working last week. I think my company has really high expectations of me because this coding stuff is really hard and they think i could do it.

Any help will be greatly appreciated.

jose
 
You'll find all you need in the System.IO nameSpace, namely the Directory and File classes in that nameSpace.

Use the Directory.GetFiles method to obtain an array of all the files in the directory and then check to see if the specific files are in that directory. Then use the Directory.GetDirectories method to obtain an array of all the subDirectories and call the function recursively on those directories.
 
This is the code I created to itereate through a directory to count the number of files that exist. You should be able to easily modify it to look for a specific file. the variable passed in called "source" is just the directory you want to search. Simply declare a FileInfo object and set the "strFileList" based on the index to the FileInfo object. Then do "objFI.FullName". Hope this helps!

Public Function BaseDirFileCount(ByVal source As String)

Dim strFileList As String() 'Collection of files in a given directory

'Enter exception block

Try

strFileList = Directory.GetFiles(source) 'Get the files

intFileCount += strFileList.Length 'Increase total file count

FileCount(source) 'Iterate through all directory's in this Base Directory

Catch ex As Exception

ProgramErrorMessage(ex)

End Try

End Function

Public Function FileCount(ByVal source As String)

Dim strFileList As String() 'Collection of files in a given directory

Dim strDirList As String() 'Collection of directories in a given directory

Dim intIndex As Integer 'Loop iterator

'Enter exception block

Try

strDirList = Directory.GetDirectories(source) 'Get the current directories

'Drill down and get the count of files in each directory and subdirectory

For intIndex = 0 To strDirList.Length - 1

strFileList = Directory.GetFiles(strDirList(intIndex))
'Current directory's files

intFileCount += strFileList.Length 'Increase file count

FileCount(strDirList(intIndex)) 'Recursive call to next subdirectory

Next

Catch ex As Exception

ProgramErrorMessage(ex)

End Try

End Function

 
Back
Top