HLP: Finding file Types

  • Thread starter Thread starter Mr. B
  • Start date Start date
M

Mr. B

I'm writing an app where I'm trying to look for and List all specific file
'types' found. So I point to a specific start top level Folder... and I want
to drill down through ALL sub folders to find file types (using File
Extenion).

All found DWG files are listed in "lbDwgList" Listbox.

I can get the top level and the 'immediate' level below the top level... but I
can't figure out how to keep going down X levels (ie: All sub-levels). Any
help appreciated!

Here's what I've go so far:

Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

Dim DWGFilPth As String ' DWG Files Path
Dim DWGfilname As String ' DWG Name
Dim DWGtestname As String ' DWG Test Name

(((#Region " Windows Form Designer generated code ")))

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' *********
' Form Load
' *********

' *********** Start Folder Paths ***********

DWGFilPth = "P:\Projects\"
' ******************************************

' Find Plot Styles in Multiple Folders and Lists them
' Get Folder Names Listing in DWGs Path
Dim rootDi As New DirectoryInfo(DWGFilPth)

' Look for DWG's in Root DWG File Path
Dim DWGdirsRt() As String = Directory.GetFiles(DWGFilPth)
For Each DWGfilname In DWGdirsRt
DWGtestname = System.IO.Path.GetFileName(DWGfilname)
' Test for DWG files
Dim FilTest As String = DWGtestname.Remove(0, (Len(DWGtestname) - 3))
If UCase(FilTest) = "DWG" Then
lbDwgList.Items.Add(DWGtestname)
End If
Next

' Look for DWG's in Sub-Folders of DWG File Root Folder
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
Dim DWGdirs() As String = Directory.GetFiles(DWGFilPth & di.Name)
For Each DWGfilname In DWGdirs
DWGtestname = System.IO.Path.GetFileName(DWGfilname)
' Test for DWG files
Dim FilTest As String = DWGtestname.Remove(0, (Len(DWGtestname) - 3))
If UCase(FilTest) = "DWG" Then
lbDwgList.Items.Add(DWGtestname)
End If
Next
Next

' *************
' End Form Load
' *************
End Sub

End Class


Regards,

Bruce
 
Hi Mr B,

You need to do it using a recursive function call. Add the following
function to your form...

\\\
Private Sub GetDWGFiles(ByVal DWGFilPth As String)
' Find Plot Styles in Multiple Folders and Lists them
' Get Folder Names Listing in DWGs Path
Dim rootDi As New DirectoryInfo(DWGFilPth)

' Look for DWG's in Root DWG File Path
Dim DWGdirsRt() As String = Directory.GetFiles(DWGFilPth)
Dim DWGfilname As String
Dim DWGtestname As String
For Each DWGfilname In DWGdirsRt
DWGtestname = System.IO.Path.GetFileName(DWGfilname)
' Look for DWG's in Root DWG File Path
If DWGtestname.Length >= 3 Then
Dim FilTest As String = DWGtestname.Remove(0,
(Len(DWGtestname) - 3))
' Test for DWG files
If UCase(FilTest) = "DWG" Then
lbDwgList.Items.Add(DWGtestname)
End If
End If
Next

' Look for DWG's in Sub-Folders of DWG File Root Folder
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
AddFilesToList(di.FullName)
Next
End Sub
///

....then call the function in your Form_Load event...

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' *********
' Form Load
' *********

GetDWGFiles("P:\Projects")

' *************
' End Form Load
' *************
End Sub
///

HTH,
Gary
 
Gary Milton said:
You need to do it using a recursive function call. Add the following
function to your form...
' Look for DWG's in Sub-Folders of DWG File Root Folder
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
AddFilesToList(di.FullName)
Next
End Sub
HTH,
Gary

Hmmmmm....

Looks like I need to work on this... Your example only displays the folders
under the Root folder :(

Oh well.. thanks for the reply... I might be able to work something out.

Regards,

Bruce
 
That was a typo - the recursion call should be...

\\\
' Look for DWG's in Sub-Folders of DWG File Root Folder
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
GetDWGFiles(di.FullName)
Next
///

Gary
 
Back
Top