3 replies
by andrew
You may want to check out Regular Expressions. Here's a link to an MSDN article about using it with VB:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvbdev99/html/VB99I1.asp
Here's some sample code that recursively searches a folder, looking for any file that contains a social security number (a pattern like '111-22-3333')
Private Sub RecurseFolders(sStartPath As String)
Dim oFSO As Scripting.FileSystemObject
Dim oFolders As Scripting.Folders
Dim oFolder As Scripting.Folder
Dim oSubFolder As Scripting.Folder
Dim oFiles As Scripting.Files
Dim oFile As Scripting.File
Dim oTs As Scripting.TextStream
Dim oRegExp As VBScript_RegExp_55.RegExp
Set oFSO = New Scripting.FileSystemObject
Set oFolder = oFSO.GetFolder(sStartPath)
Set oFiles = oFolder.Files
Set oRegExp = New VBScript_RegExp_55.RegExp
oRegExp.Pattern = "[0-9]{3}\-[0-9]{2}\-[0-9]{4}"
For Each oFile In oFiles
Set oTs = oFile.OpenAsTextStream(ForReading) Do
If oRegExp.Test(oTs.ReadLine()) Then
List1.AddItem oFile.Path & "\" & oFile.Name
Exit Do
End If
Loop Until oTs.AtEndOfStream
oTs.Close
Next
Set oFolders = oFolder.SubFolders
For Each oSubFolder In oFolders
RecurseFolders oSubFolder.Path
Next
End Sub
Perl is a great language to regular expressions. O'Reilly's book is excellent:
http://www.oreilly.com/catalog/regex/
Hope this is what you're looking for.
(make sure there are no spaces in the URLs)
by tato
by tato
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending