'VB.Net Example - How to recursively iterate through all folders on the computer..
'
'
'(c) Jon Vote 07/2002
'
'Idioma Software Inc.
'www.idioma-software.com
'jon@idioma-software.com
'
'This example may be freely used in code
'as long as credit is given to the author.
'No publication is authorized.
'
'Code is presented as is.
'User assumes all responsibility
'1) Start a new VB.Net project. Form1 is created by default.
'2) Press F7 to bring up the code window.
'3) Replace all (some? no all!) of the code in the code window
' with the following:
'
'VB.Net Example - How to recursively iterate through all folders on the computer..
'
'
'(c) Jon Vote 07/2002
'
'Idioma Software Inc.
'www.idioma-software.com
'jon@idioma-software.com
'
'This example may be freely used in code
'as long as credit is given to the author.
'No publication is authorized.
'
'Code is presented as is.
'User assumes all responsibility
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnGo As System.Windows.Forms.Button
Friend WithEvents txtStartWith As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents grpIncludeAttributes As System.Windows.Forms.GroupBox
Friend WithEvents chkArchive As System.Windows.Forms.CheckBox
Friend WithEvents chkDirectory As System.Windows.Forms.CheckBox
Friend WithEvents chkHidden As System.Windows.Forms.CheckBox
Friend WithEvents chkNormal As System.Windows.Forms.CheckBox
Friend WithEvents chkVolume As System.Windows.Forms.CheckBox
Friend WithEvents chkSystem As System.Windows.Forms.CheckBox
Friend WithEvents chkReadOnly As System.Windows.Forms.CheckBox
Friend WithEvents lblOutput As System.Windows.Forms.Label
Friend WithEvents chkIncludeSubFolders As System.Windows.Forms.CheckBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnGo = New System.Windows.Forms.Button()
Me.txtStartWith = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.grpIncludeAttributes = New System.Windows.Forms.GroupBox()
Me.chkVolume = New System.Windows.Forms.CheckBox()
Me.chkSystem = New System.Windows.Forms.CheckBox()
Me.chkReadOnly = New System.Windows.Forms.CheckBox()
Me.chkNormal = New System.Windows.Forms.CheckBox()
Me.chkHidden = New System.Windows.Forms.CheckBox()
Me.chkDirectory = New System.Windows.Forms.CheckBox()
Me.chkArchive = New System.Windows.Forms.CheckBox()
Me.lblOutput = New System.Windows.Forms.Label()
Me.chkIncludeSubFolders = New System.Windows.Forms.CheckBox()
Me.grpIncludeAttributes.SuspendLayout()
Me.SuspendLayout()
'
'btnGo
'
Me.btnGo.Location = New System.Drawing.Point(12, 176)
Me.btnGo.Name = "btnGo"
Me.btnGo.TabIndex = 0
Me.btnGo.Text = "Go"
'
'txtStartWith
'
Me.txtStartWith.Location = New System.Drawing.Point(68, 8)
Me.txtStartWith.Name = "txtStartWith"
Me.txtStartWith.Size = New System.Drawing.Size(272, 20)
Me.txtStartWith.TabIndex = 1
Me.txtStartWith.Text = "c:\"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(4, 12)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(60, 12)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Start With:"
'
'grpIncludeAttributes
'
Me.grpIncludeAttributes.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkVolume, Me.chkSystem, Me.chkReadOnly, Me.chkNormal, Me.chkHidden, Me.chkDirectory, Me.chkArchive})
Me.grpIncludeAttributes.Location = New System.Drawing.Point(8, 41)
Me.grpIncludeAttributes.Name = "grpIncludeAttributes"
Me.grpIncludeAttributes.Size = New System.Drawing.Size(340, 124)
Me.grpIncludeAttributes.TabIndex = 3
Me.grpIncludeAttributes.TabStop = False
Me.grpIncludeAttributes.Text = "Include Attributes"
'
'chkVolume
'
Me.chkVolume.Checked = True
Me.chkVolume.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkVolume.Location = New System.Drawing.Point(184, 72)
Me.chkVolume.Name = "chkVolume"
Me.chkVolume.Size = New System.Drawing.Size(128, 20)
Me.chkVolume.TabIndex = 6
Me.chkVolume.Text = "Volume"
'
'chkSystem
'
Me.chkSystem.Checked = True
Me.chkSystem.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkSystem.Location = New System.Drawing.Point(184, 48)
Me.chkSystem.Name = "chkSystem"
Me.chkSystem.Size = New System.Drawing.Size(128, 20)
Me.chkSystem.TabIndex = 5
Me.chkSystem.Text = "System"
'
'chkReadOnly
'
Me.chkReadOnly.Checked = True
Me.chkReadOnly.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkReadOnly.Location = New System.Drawing.Point(184, 24)
Me.chkReadOnly.Name = "chkReadOnly"
Me.chkReadOnly.Size = New System.Drawing.Size(128, 20)
Me.chkReadOnly.TabIndex = 4
Me.chkReadOnly.Text = "ReadOnly"
'
'chkNormal
'
Me.chkNormal.Checked = True
Me.chkNormal.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkNormal.Location = New System.Drawing.Point(24, 96)
Me.chkNormal.Name = "chkNormal"
Me.chkNormal.Size = New System.Drawing.Size(128, 20)
Me.chkNormal.TabIndex = 3
Me.chkNormal.Text = "Normal"
'
'chkHidden
'
Me.chkHidden.Checked = True
Me.chkHidden.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkHidden.Location = New System.Drawing.Point(24, 72)
Me.chkHidden.Name = "chkHidden"
Me.chkHidden.Size = New System.Drawing.Size(128, 20)
Me.chkHidden.TabIndex = 2
Me.chkHidden.Text = "Hidden"
'
'chkDirectory
'
Me.chkDirectory.Checked = True
Me.chkDirectory.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkDirectory.Location = New System.Drawing.Point(24, 48)
Me.chkDirectory.Name = "chkDirectory"
Me.chkDirectory.Size = New System.Drawing.Size(128, 20)
Me.chkDirectory.TabIndex = 1
Me.chkDirectory.Text = "Directory"
'
'chkArchive
'
Me.chkArchive.Checked = True
Me.chkArchive.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkArchive.Location = New System.Drawing.Point(24, 24)
Me.chkArchive.Name = "chkArchive"
Me.chkArchive.Size = New System.Drawing.Size(128, 20)
Me.chkArchive.TabIndex = 0
Me.chkArchive.Text = "Archive"
'
'lblOutput
'
Me.lblOutput.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.lblOutput.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblOutput.Location = New System.Drawing.Point(0, 208)
Me.lblOutput.Name = "lblOutput"
Me.lblOutput.Size = New System.Drawing.Size(352, 20)
Me.lblOutput.TabIndex = 4
'
'chkIncludeSubFolders
'
Me.chkIncludeSubFolders.Checked = True
Me.chkIncludeSubFolders.CheckState = System.Windows.Forms.CheckState.Checked
Me.chkIncludeSubFolders.Location = New System.Drawing.Point(104, 176)
Me.chkIncludeSubFolders.Name = "chkIncludeSubFolders"
Me.chkIncludeSubFolders.Size = New System.Drawing.Size(208, 16)
Me.chkIncludeSubFolders.TabIndex = 5
Me.chkIncludeSubFolders.Text = "Include Sub-Folders"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(352, 229)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkIncludeSubFolders, Me.lblOutput, Me.grpIncludeAttributes, Me.Label1, Me.txtStartWith, Me.btnGo})
Me.Name = "Form1"
Me.Text = "Iterate Directories Example - www.skycoder.com"
Me.grpIncludeAttributes.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Try
CheckCheckBoxes()
IterateDirectory(txtStartWith.Text)
Catch
MsgBox(Err.Description)
End Try
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
End Sub
Private Sub IterateDirectory(ByVal strPath As String)
Dim atrIncludeThese As FileAttribute
Dim strName As String
Dim i As Integer
'Set the attributes for which we are searching
If chkArchive.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.Archive
End If
If chkDirectory.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.Directory
End If
If chkHidden.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.Hidden
End If
If chkNormal.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.Normal
End If
If chkReadOnly.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.ReadOnly
End If
If chkSystem.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.System
End If
If chkVolume.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.Volume
End If
'Iterate for each entry that matches
'the criteria
strName = Dir$(strPath, atrIncludeThese)
Do While strName <> ""
strName = PutSlash(strPath) & strName
PutStatus(strName)
Debug.WriteLine(strName)
strName = Dir$()
Loop
'Recurse for each subfolder
'if we are searching sub-folders
If chkIncludeSubFolders.Checked Then
Dim colDirectories As New Collection()
atrIncludeThese = FileAttribute.Directory
If chkHidden.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.Hidden
End If
If chkSystem.Checked Then
atrIncludeThese = atrIncludeThese + FileAttribute.System
End If
'Iterate for each folder
strName = Dir$(strPath, FileAttribute.Directory)
If strName <> "" Then
Do While strName <> ""
If GetAttr(PutSlash(strPath) & strName) = FileAttribute.Directory Then
colDirectories.Add(PutSlash(PutSlash(strPath) & strName))
End If
strName = Dir$()
Loop
For i = 1 To colDirectories.Count
IterateDirectory(colDirectories.Item(i))
Next
End If
End If
End Sub
'CheckCheckBoxes: Checks chkNormal if nothing checked
Private Sub CheckCheckBoxes()
Dim x As Control
Dim chkCheck As CheckBox
Dim bOneChecked As Boolean
For Each x In Me.Controls
If TypeOf x Is CheckBox Then
chkCheck = x
If chkCheck.Checked Then
bOneChecked = True
Exit For
End If
End If
Next
If Not bOneChecked Then
chkNormal.Checked = True
End If
End Sub
'PutSlash: Insures there is a closing slash
Private Function PutSlash(ByVal strPathName As String) As String
PutSlash = IIf(Microsoft.VisualBasic.Right(strPathName, 1) = "\", strPathName, strPathName & "\")
End Function
Private Sub PutStatus(ByVal strPutThis As String)
lblOutput.Text = strPutThis
System.Windows.Forms.Application.DoEvents()
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
End
End Sub
End Class