|
-
July 25th, 2008, 02:12 PM
#1
Finding strings in multiple files
Hi,
Can some one please show me, if possible, how to read several files, looking for a certain number,
for example, you might have:
text1.txt
text2.txt
text3.txt
one of these will contain a number, in this example 5, is there a way of finding out wish one of the files contains 5.
Thanks, any questions please ask
-
July 25th, 2008, 02:21 PM
#2
Re: Finding strings in multiple files
If you look at some of the threads from today, you'll find most of your answer. All you have to do is look at more than one file, but that's simple enough.
Here are some relevant threads, all from today:
http://www.codeguru.com/forum/showthread.php?t=457987
http://www.codeguru.com/forum/showthread.php?t=457784
http://www.codeguru.com/forum/showthread.php?t=457952
Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.
*Guarantee may not be honoured.
-
July 25th, 2008, 02:44 PM
#3
Re: Finding strings in multiple files
Seems to me, that you could store the file names in a string array, loop through them, and within that loop, you'd open each, search for the text, and close the file. Searching could be one line at a time, or all at once. If the string being searched for is found, exit the loop.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
July 25th, 2008, 03:06 PM
#4
Re: Finding strings in multiple files
Could you please give me an example code, because i know how to read a .txt file but new to arrays.
thanks
-
July 25th, 2008, 03:22 PM
#5
Re: Finding strings in multiple files
You do not even need an array, just read each line using somethign like line input or read the entire file into a variable then use instr() to search for the number a result > 0 means it has been found.
-
July 25th, 2008, 06:59 PM
#6
Re: Finding strings in multiple files
Something Like this?
Code:
Public Function SearchFiles(ByVal SearchString as String, ParamArray _
FilePaths() as Variant) as Variant
Dim strFilePathsWithString() as String
Dim intCounter as Integer
'initialize counter
intCounter = 0
'loop through passed files
For i = 0 to Ubound(FilePaths)
Dim strFileContents as string 'To recieve contents of file
Dim intFileNum as Integer 'Used to reference Open file.
'set file number
intFileNum = FreeFile(0)
'Open file at index i
Open FilePaths(i) for Input as intFileNum
'Loop through creating your full file string
Do Until EOF(intFileNum)
Dim strLineRead as string 'Used to read a line
'get line from file
Line Input #intFileNum, strLineRead
'Add the Line to your combined file contents.
strFileContents = strFileContents & strLineRead
Loop
'Close the file
Close intFileNum
'Test to see if search string is in contents of file
if Instr(strFileContents, SearchString) > 0 then
'redimension strFilePathsWithString to counter + 1
Redim Preserve FilePathsWithString(intCounter + 1)
'add the file path for the file that has it to the string to the
'strFilePathsWithString array
strFilePathsWithString(counter) = cstr(FilePaths(i))
'increment the counter
intCounter = intCounter + 1
End If
Next
'set function equal to created array of files with the searched for string
SearchFiles = strFilePathsWithString
End Function
There may be some errors in that; I have a hard time coding with out my color coded text editor, but this I think would be a good structure. In addition, there may be a more efficient way to do this; I am still kind of a noob myself, so one of the people with more experience may be able to clean it up.
Last edited by VehementSoftware; July 25th, 2008 at 07:41 PM.
Reason: Forgot to add close statement.
-
July 25th, 2008, 09:57 PM
#7
Re: Finding strings in multiple files
Actually if you are going to do a line by line read then the best thing to do is check each line for the search phrase and if it is found exit the loop and close the file. No need to save all the lines and then do the search unless you want to locate all the instances in the file.
Code:
open filename for input as #filehandle
do while not eof(filehandle)
line input #filehandle, LineFromFile
if Instr(LineFromFile,SearchPhrase) then
FoundIt=True
exit do
end if
loop
close #filehandle
If foundit is false then move on to the next file and do the same process ideally using an outter loop with something like above nested within.
-
July 25th, 2008, 10:51 PM
#8
Re: Finding strings in multiple files
then, in six months, they ask you to count how many number X in each file?
back to square one?
-
July 25th, 2008, 11:02 PM
#9
Re: Finding strings in multiple files
I like the line by line check with a break as well.
-
July 26th, 2008, 04:53 AM
#10
Re: Finding strings in multiple files
That's basically it VehementSoftware, though it isn't necessary to use Variants. It is always a good idea to avoid Variants whenever possible, as they are less efficient. In this case, a string array would work well.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
July 26th, 2008, 09:36 AM
#11
Re: Finding strings in multiple files
 Originally Posted by dglienna
then, in six months, they ask you to count how many number X in each file?
back to square one?
No biggie just a couple of minor tweaks. change the one line from foundit=true to foundit=foundit+1 and comment out the exit do and now you have a counter for how many lines it occurs in. If it could occur more than once in a line then another simple little loop around the instr and foundit lines that will check for all instances within the line.
However if the intention was to find all instances of it within each specific file then I would not use a line by line read as this would not be very efficent and could be slow on a large file. Instead I would read the entire file into memory in one shot and search the content.
-
July 26th, 2008, 03:19 PM
#12
Re: Finding strings in multiple files
 Originally Posted by DataMiser
Actually if you are going to do a line by line read then the best thing to do is check each line for the search phrase and if it is found exit the loop and close the file. No need to save all the lines and then do the search unless you want to locate all the instances in the file.
Code:
open filename for input as #filehandle
do while not eof(filehandle)
line input #filehandle, LineFromFile
if Instr(LineFromFile,SearchPhrase) then
FoundIt=True
exit do
end if
loop
close #filehandle
If foundit is false then move on to the next file and do the same process ideally using an outter loop with something like above nested within.
Thank you very much for that, unfortunatly, i basically have a file like the following:
AAAAAA 1.1.1
AAAAAB 1.1.1
AAAAAC 1.1.1
AAAAAA 1.1.2
AAAAAB 1.1.2
AAAAAA 1.1.2
What i wish to do though, is detect the newest times, in this example 1.1.2 and then find out how many different strings there are, for exmaple the newest time in my little example is 1.1.2, then i want it to say there were two different strings, AAAAAA,AAAAAB, but not detect the second AAAAAA, resulting in a textbox on the .exe = 2. Is this possible.
Thanks
-
July 26th, 2008, 03:49 PM
#13
Re: Finding strings in multiple files
Here's my very hastily thrown together solution - If you want anything explained, just asked. It only works for one file, but doubtless you can use the earlier advice to adapt it. It also will only work with the prototpye as you gave it in your last post. It isn't a very good solution over all (feel free to laugh at my attempt at error checking) but It should get the job done!
I copied your sample data into a text file, then tried this with:
Code:
Debug.Print Join(Form1.GetLinesAtLastTime("C:\text.txt"), vbNewLine)
As the debug window doesn't like arrays. It returned the correct data.
So, here we go!
Code:
Option Explicit
Private Type mTime
p1 As Integer
p2 As Integer
p3 As Integer
End Type
Private Function mTimeSame(ByRef mTime1 As mTime, ByRef mTime2 As mTime) As Boolean
mTimeSame = ((mTime1.p1 = mTime2.p1) And (mTime1.p2 = mTime2.p2) And (mTime1.p3 = mTime2.p3))
End Function
Private Function mTimeOneIsBigger(ByRef mTime1 As mTime, ByRef mTime2 As mTime) As Boolean
If mTime1.p1 > mTime2.p1 Then
mTimeOneIsBigger = True
ElseIf mTime1.p1 < mTime2.p1 Then
mTimeOneIsBigger = False
Else
If mTime1.p2 > mTime2.p2 Then
mTimeOneIsBigger = True
ElseIf mTime2.p2 < mTime2.p2 Then
mTimeOneIsBigger = False
Else
If mTime1.p3 > mTime2.p3 Then
mTimeOneIsBigger = True
ElseIf mTime1.p3 < mTime2.p1 Then
mTimeOneIsBigger = False
Else
mTimeOneIsBigger = False
End If
End If
End If
End Function
Public Function GetLinesAtLastTime(ByVal FileName As String) As String()
Dim mFile As Integer, Line As String
Dim Time As mTime, NewTime As mTime
Dim Parts() As String, Data As String
'Prototype: AAAAAA 1.1.1
mFile = freeFile
Open FileName For Input As #mFile
Do While Not EOF(mFile)
Line Input #mFile, Line
Parts() = Split(Line, " ")
If UBound(Parts) = 1 Then
Parts() = Split(Parts(1), ".")
If UBound(Parts) = 2 Then
NewTime.p1 = Parts(0)
NewTime.p2 = Parts(1)
NewTime.p3 = Parts(2)
Else
Err.Raise 1342, "Some Code", "ASGEGWRH!"
Else
Err.Raise 1342, "Some Code", "ASGEGWRH!"
End If
Parts = Split(Line, " ")
If mTimeSame(Time, NewTime) Then
Data = Data & ";" & Parts(0)
ElseIf mTimeOneIsBigger(NewTime, Time) Then
Time = NewTime
Data = Parts(0)
End If
Loop
Close #mFile
GetLinesAtLastTime = Split(Data, ";")
End Function
Hope that's of some help!
Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.
*Guarantee may not be honoured.
-
July 26th, 2008, 04:26 PM
#14
Re: Finding strings in multiple files
There is a problem with the line
'Private Function mTimeSame(ByRef mTime1 As mTime, ByRef mTime2 As mTime) As Boolean'
it comes up with the error 'User-Defined type not defined' could you explain why?
thanks
-
July 26th, 2008, 04:28 PM
#15
Re: Finding strings in multiple files
Have you got that function in the same form / module at the definition of mTime? If you dump all of that code into one module, it should work.
The reason for the error is that the compiler couldn't find one of the types in the line. I won't be boolean as that is a core value which you couldn't program without if you tried, so it means it can't find the type mTime. The likely reason, as I sort of mentioned, is that if it's in a different form or module and declared as private (as it was in my code ("Private Type mTime"), then it can't be used outside that Form / Module.
In fact, you can't have Types, Enums (enumerators), Declares and (if memory serves) constants as Public members of a form. That's why you'll find many projects have module full of only them.
Last edited by javajawa; July 26th, 2008 at 04:39 PM.
Help from me is always guaranteed!*
VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.
*Guarantee may not be honoured.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|