shivermetimbers
asked on
Need help iterating directories
Hello,
I need to write a program that will sweep through a directory (e.g. C:\Program Files\My Software) and any sub-directories and return an array of all the files it finds.
If anyone could point me in the right direction it would be much appreciated.
Many thanks,
Shivermetimbers.
I need to write a program that will sweep through a directory (e.g. C:\Program Files\My Software) and any sub-directories and return an array of all the files it finds.
If anyone could point me in the right direction it would be much appreciated.
Many thanks,
Shivermetimbers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Shivermetimbers...
YOu can use a recursice function and use the FileSystemObject...
In VB6.0
This code will use the filesystem object, and recursively scan the folders, I wrote it to add the files to a treeview
in the declaration section
private m_files() as string
' in another function
Sub main()
Dim m_fso As New FileSystemObject
Dim m_folder As Folder
Set m_folder = m_fso.getFolder(Me.txtPath .Text)
scanFiles(m_folder)
End Sub
Private Sub scanFiles(thisFolder As Folder, optional extension As String = "")
' recursive function to scan the tree below the current path and
' load the files into the tree
' if extension is specified then filter
dim i as long
Dim m_file As File
Dim m_addFile As Boolean
For Each m_file In thisFolder.Files
' add this file to the tree view
' simple compare, it extension mathces the file extension set the flag,
' if not extension is given then add the file.
If Len(extension) > 0 Then
If StrComp(Right(m_file.Name, Len(extension)), extension, vbTextCompare) <> 0 Then
' don't process this
m_addFile = False
Else
m_addFile = True
End If
Else
m_addFile = True
End If
' can we add this file to the array
If m_addFile Then
' get the array size and increment.
on error resume next
i = ubound(m_files)
if err.number <> 0 then
i = 0
err.clear
End If
i = 1 + 1
' reinstate the error handler
on error goto 0
' load the file details
redim preserve m_files(i)
m_files(i) = m_file.Name
End If
Next
Dim m_folder As Folder
For Each m_folder In thisFolder.SubFolders
scanFiles m_folder, extension
Next
End Sub
I hope this helps
Dreffed
YOu can use a recursice function and use the FileSystemObject...
In VB6.0
This code will use the filesystem object, and recursively scan the folders, I wrote it to add the files to a treeview
in the declaration section
private m_files() as string
' in another function
Sub main()
Dim m_fso As New FileSystemObject
Dim m_folder As Folder
Set m_folder = m_fso.getFolder(Me.txtPath
scanFiles(m_folder)
End Sub
Private Sub scanFiles(thisFolder As Folder, optional extension As String = "")
' recursive function to scan the tree below the current path and
' load the files into the tree
' if extension is specified then filter
dim i as long
Dim m_file As File
Dim m_addFile As Boolean
For Each m_file In thisFolder.Files
' add this file to the tree view
' simple compare, it extension mathces the file extension set the flag,
' if not extension is given then add the file.
If Len(extension) > 0 Then
If StrComp(Right(m_file.Name,
' don't process this
m_addFile = False
Else
m_addFile = True
End If
Else
m_addFile = True
End If
' can we add this file to the array
If m_addFile Then
' get the array size and increment.
on error resume next
i = ubound(m_files)
if err.number <> 0 then
i = 0
err.clear
End If
i = 1 + 1
' reinstate the error handler
on error goto 0
' load the file details
redim preserve m_files(i)
m_files(i) = m_file.Name
End If
Next
Dim m_folder As Folder
For Each m_folder In thisFolder.SubFolders
scanFiles m_folder, extension
Next
End Sub
I hope this helps
Dreffed
The below works for me. Returned the same # of files in the "system32" folder as it does when going to "properties". It's recursive and I just pass the files around as chunks of strings (thought of trying arrays but I'd have to combine them then). As for the reason of "pagefile.sys", it errors when checking it's attribute so I had to ignore it. :] You can modify the attributes to search for also (I decided to have it return system files/etc).
Form1:
---------------
Option Explicit
Private Sub Form_Load()
Dim sArray() As String, lLoop As Long
sArray = Split(DirSearchAll("C:\win dows\syste m32\"), vbCrLf)
For lLoop = LBound(sArray) To UBound(sArray)
'Print files 1 by 1 to debug window
Debug.Print sArray(lLoop)
Next lLoop
MsgBox UBound(sArray) & " files found!"
End Sub
Private Function DirSearch(ByVal sSearchDir As String, bDir As Boolean) As String
Dim sDir As String, sFind As String
sDir = Dir(sSearchDir, vbDirectory + vbHidden + vbNormal + vbSystem + vbReadOnly)
Do Until sDir = vbNullString
If sDir <> "." And sDir <> ".." Then
If bDir = True And sDir <> "pagefile.sys" Then
If GetAttr(sSearchDir & sDir) And vbDirectory Then
DirSearch = DirSearch & sSearchDir & sDir & vbCrLf
End If
ElseIf bDir = False And sDir <> "pagefile.sys" Then
If Not GetAttr(sSearchDir & sDir) And vbDirectory Then
DirSearch = DirSearch & sSearchDir & sDir & vbCrLf
End If
End If
End If
sDir = Dir()
DoEvents
Loop
End Function
Private Function DirSearchAll(ByVal sSearchDir As String) As String
Dim sFiles As String, sDirs As String
Dim sDirsSplit() As String, lLoop As Integer
sFiles = DirSearch(sSearchDir, False)
sDirs = DirSearch(sSearchDir, True)
sDirsSplit = Split(sDirs, vbCrLf)
For lLoop = LBound(sDirsSplit) To UBound(sDirsSplit) - 1
sFiles = sFiles & DirSearchAll(sDirsSplit(lL oop) & "\")
Next lLoop
DirSearchAll = sFiles
End Function
Form1:
---------------
Option Explicit
Private Sub Form_Load()
Dim sArray() As String, lLoop As Long
sArray = Split(DirSearchAll("C:\win
For lLoop = LBound(sArray) To UBound(sArray)
'Print files 1 by 1 to debug window
Debug.Print sArray(lLoop)
Next lLoop
MsgBox UBound(sArray) & " files found!"
End Sub
Private Function DirSearch(ByVal sSearchDir As String, bDir As Boolean) As String
Dim sDir As String, sFind As String
sDir = Dir(sSearchDir, vbDirectory + vbHidden + vbNormal + vbSystem + vbReadOnly)
Do Until sDir = vbNullString
If sDir <> "." And sDir <> ".." Then
If bDir = True And sDir <> "pagefile.sys" Then
If GetAttr(sSearchDir & sDir) And vbDirectory Then
DirSearch = DirSearch & sSearchDir & sDir & vbCrLf
End If
ElseIf bDir = False And sDir <> "pagefile.sys" Then
If Not GetAttr(sSearchDir & sDir) And vbDirectory Then
DirSearch = DirSearch & sSearchDir & sDir & vbCrLf
End If
End If
End If
sDir = Dir()
DoEvents
Loop
End Function
Private Function DirSearchAll(ByVal sSearchDir As String) As String
Dim sFiles As String, sDirs As String
Dim sDirsSplit() As String, lLoop As Integer
sFiles = DirSearch(sSearchDir, False)
sDirs = DirSearch(sSearchDir, True)
sDirsSplit = Split(sDirs, vbCrLf)
For lLoop = LBound(sDirsSplit) To UBound(sDirsSplit) - 1
sFiles = sFiles & DirSearchAll(sDirsSplit(lL
Next lLoop
DirSearchAll = sFiles
End Function
http://oldlook.experts-exchange.com/questions/20923282/Search-and-Replace-in-Multiple-Sub-Directories.html
HTH