Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scan directories for html files

Status
Not open for further replies.

Gti

Programmer
Jul 23, 2001
99
PT
How can i search for HTML files in a directory with sub-directories???

Ex.:
ScanSource
|_ Test1
|_ Test2

I Have HTML files in the two test folder and my start point is the Scansource Directory How can i scan the files inside the two folders????

:) tks

 
Hi,

That sounded easy, I thought that you could use the dir command to get the folders, but it doesen't work recursively. Anyway a combination of dir and the filesystemobject and a recurvise call does the trick:

------------------------------------------------------------
'Add a commandbutton and a list to a from and paste this code into the form.
Option Explicit
Dim LocalFileSystem

Private Sub Command1_Click()
Set LocalFileSystem = CreateObject("Scripting.FileSystemObject")
ScanDir "c:\"
End Sub

Private Sub ScanDir(TheDir As String)
Dim AFolder As Variant, TheFolders As Variant
Dim CurFile As String
'list all the htm files in this dir
CurFile = Dir(TheDir & "\*.htm")
While CurFile <> &quot;&quot;
List1.AddItem TheDir & &quot;\&quot; & CurFile
CurFile = Dir
Wend
Set TheFolders = LocalFileSystem.GetFolder(TheDir)
For Each AFolder In TheFolders.SubFolders
ScanDir (AFolder)
Next
End Sub
-----------------------------------------------------------

*:->*Sunaj
 
Once you make the decision to use the FileSystemObject, then it is pretty straightforward. Here's one method (note that it's pretty similar to sunaj's solution, but sticks with one file handling model, rather than two); you'll need to add a reference to the Microsoft Scripting Runtime. As with sunaj's code, add a listbox and command button to a form:
[tt]
Option Explicit
Dim fso As New FileSystemObject
Dim myFolder As Folder
Dim myfile As File

Private Sub Command1_Click()
ScanFolderForFiles &quot;C:\WorkShop&quot; ' Or whatever...
End Sub


Private Sub ScanFolderForFiles(strFolder As String)
Dim tempFolder As Folder

Set myFolder = fso.GetFolder(strFolder) 'Root of current recursion ppoint

' List all the files in the current folder
For Each myfile In myFolder.Files
List1.AddItem (myfile.Path)
Next

' Examine all the subfolders of the current folder
For Each tempFolder In myFolder.SubFolders
ScanFolderForFiles tempFolder.Path
Next

End Sub
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top