LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Searching csv files for particular string

Iain,
If it is just a text search then you can amend the code below to suit. Note
you should add checks/error traps.
Also, if your file are massive (~50MB, depending on hardware), you may have
to split the file reading into chunks.

If you require information more like column/row, post back.

Private Sub CommandButton4_Click()
MsgBox "Total matches = " & SearchFile("Some Text")
End Sub


Public Function SearchFile(TextToFind As String, _
Optional LogFile As String = "C:\LogFile.txt", _
Optional FileFilter As String = "CSV Files
(*.csv), *.csv") _
As Long

Dim FileNames As Variant
Dim FileIn As Integer
Dim FileOut As Integer
Dim i As Long
Dim FileText As String
Dim LastPosition As Long
Dim FoundCountThisFile As Long
Dim FoundCountTotal As Long

FileNames = Application.GetOpenFilename(FileFilter, , , , True)

If Not IsArray(FileNames) Then Exit Function

FileOut = FreeFile
Open LogFile For Append As #FileOut
Print #FileOut, "Started: " & Format(Now(), "dd/mmmm/yyyy h:mm")
Print #FileOut, "Searching for: " & TextToFind

FileIn = FreeFile
For i = LBound(FileNames) To UBound(FileNames)
Open FileNames(i) For Input As #FileIn
Print #FileOut, "Processing file: " & FileNames(i)

FileText = Input(LOF(FileIn), #FileIn)

LastPosition = InStr(1, FileText, TextToFind)
Do While LastPosition 0
FoundCountThisFile = FoundCountThisFile + 1
Print #FileOut, LastPosition;
LastPosition = InStr(LastPosition + 1, FileText, TextToFind)
Loop
FoundCountTotal = FoundCountTotal + FoundCountThisFile
Print #FileOut, ""
Print #FileOut, "Total found in this file: " & FoundCountThisFile
Print #FileOut, "----------------"
Close #FileIn
FoundCountThisFile = 0
Next

Print #FileOut, "Total found in all files: " & FoundCountTotal
Print #FileOut, "Finished: " & Format(Now(), "dd/mmmm/yyyy h:mm")
Print #FileOut, "------------------------------------------------"

Close #FileOut

SearchFile = FoundCountTotal

End Function

NickHK

"Iain M" wrote in message
...
I have a requirement to search a number of csv files for a particular

string.
I would prefer to be able to input this string as it will vary for each
search. I would then like to concatenate each of the successful finds

into
one csv file.

All help greatly appreciated.



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching csv files for particular string Iain M Excel Programming 0 January 22nd 07 10:41 AM
Searching for string in other cells Ruatha Excel Worksheet Functions 3 June 9th 06 10:42 PM
searching through a string? funkymonkUK[_144_] Excel Programming 6 May 3rd 06 02:19 PM
Searching a String dcstech Excel Programming 2 April 6th 04 02:41 PM
searching a string with a partial string dcstech Excel Programming 1 April 5th 04 10:57 PM


All times are GMT +1. The time now is 05:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"