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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for a string in a directory of files - please help

Status
Not open for further replies.

Maggie24

IS-IT--Management
Jun 29, 2004
36
IE
Hi all,

I want to search through about 100 files for a specific string - "Country code is invalid" which is contained in the text of the file, these are xml files.

I am familiar and have been using the FSO to move and copy files but i just cannot find the property/object to search for a string contained in the text of the file,

If any can point me in the right direction - i would be very gratful as i have been searching the net all morn and am at my wits end !!

Thanking you all,

Maggie

"Work is the curse of the drinking classes
 
Maggie,

I think you are going to be reduced to opening the files for read access, and then manually scanning through them.

When I get some free time, I might have a bash at writing a DLL for this... I know we've done something very similar to this at work, but it was a different programmer (manager, actually) that did it.

mmilan
 
Here is a solution that uses Regular Expressions and the file system object. You also could use the Instr function to do this as well. I am sure that my RegExp solution (specifically the pattern) is not going to be the best but the concept is show here. Maybe a RegExp guru (errm, strongm, Dr. J) could provide feedback. Anyway, here is my attemtp. You need to add a reference to the Microsoft VBScript Regular Expression 5.5 library.

Code:
    Dim fso As New Scripting.FileSystemObject
    Dim lFolder As Scripting.Folder
    Dim lFile As Scripting.File
    Dim ltxtStream As Scripting.TextStream
    
    Dim strToMatch As String
    Dim myRE As RegExp
    Dim myMatches As MatchCollection
    Dim lMatch As Match
    
    Set lFolder = fso.GetFolder("C:\TestFiles")
    
    strToMatch = "127346"
    
    For Each lFile In lFolder.Files
        'open a text stream
        Set ltxtStream = fso.OpenTextFile(lFile.Path)
        
        Set myRE = New RegExp
        
        myRE.IgnoreCase = False
        myRE.Pattern = strToMatch
        myRE.Global = True
        
        Set myMatches = myRE.Execute(ltxtStream.ReadAll)
        
        For Each lMatch In myMatches
            Debug.Print lMatch.Value & " occurs in " & lFile.Path & " starting at position " & lMatch.FirstIndex
        Next
        
        ltxtStream.Close
        Set ltxtStream = Nothing
    Next
 
Your Instr idea is a lot simpler, but everyone (myself DEFINITELY included) could use exposure to RegExps...

mmilan
 
Here is the Instr solution:

Code:
    Dim fso As New Scripting.FileSystemObject
    Dim lFolder As Scripting.Folder
    Dim lFile As Scripting.File
    Dim ltxtStream As Scripting.TextStream
    
    Dim strToMatch As String
    Dim myRE As RegExp
    Dim myMatches As MatchCollection
    Dim lMatch As Match
    Dim strFile As String
    Dim lPos As Long
        
    Set lFolder = fso.GetFolder("C:\TestFiles")
    
    strToMatch = "127346"
    
    For Each lFile In lFolder.Files
        'open a text stream
        Set ltxtStream = fso.OpenTextFile(lFile.Path)
        
        strFile = ltxtStream.ReadAll
        
        lPos = InStr(1, strFile, strToMatch)
        If lPos > 0 Then
            'there is a match found
            Do
                Debug.Print strToMatch & " occurrs in " & lFile.Path & " starting at position " & lPos
                lPos = InStr(lPos + Len(strToMatch), strFile, strToMatch)
            Loop While lPos > 0
        End If
        
        ltxtStream.Close
        Set ltxtStream = Nothing
    Next
 
And, it appears after some initial tests that the Instr solution is quicker especially when there is a lot of file and/or if the file is large is size.
 
Hi bjd4dc and m milan, thanks for responding,

bjd4dc - thanks for your solution - it looks like it will definitely do the trick - i will let you know how i get on,

again thanks for your time,

Maggie

"Work is the curse of the drinking classes
 
Hi all,
BJD - I have used you Instr code and it is working fine except for one small thing, in my program, if a file contains the strtoMatch it is copied to another folder and deleted from the original folder.

The file is copied with no problems, however when i try to delete the file from the original a
"Permission Denied"
error occurs.

I need to be able to delete the file from the original - does anyone have any ideas as to why this occuring?? Any help would be great

This is my code

Set oFolder = oFSO.GetFolder(BatchStateArch(g_cCISInboundNACKSDirectory))

CountryInvalid = "Country"

For Each oFile In oFolder.Files
'open a text stream
Set ltxtStream = oFSO.OpenTextFile(oFile.Path)

strFile = ltxtStream.ReadAll
MsgBox (strFile)
lPos = InStr(1, strFile, CountryInvalid)
If lPos > 0 Then
'there is a match found

Call oFSO.CopyFile(BatchStateArch(g_cCISInboundNACKSDirectory) & "\" & oFile.Name, (PathErrorCat) & Country & "\" & oFile.Name, True)
Call oFSO.DeleteFile(BatchStateArch(g_cCISInboundNACKSDirectory) & "\" & oFile.Name)

End If

ltxtStream.Close
Set ltxtStream = Nothing
Next




"Work is the curse of the drinking classes
 
Try closing the text stream before you try to delete the file.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
bjd4dc and Tom,

Thanks for all your help,

the code now works perfectly,

Thanks again,

Maggie

"Work is the curse of the drinking classes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top