Author |
Message |
Kevin Well #1 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
I need a little help. I have been searching and searching for an answer to this and have come up with zilch. If I have a file called "C:\test.txt" and the files consists of the following: ------ Goodbye Hello Next Year ------ How you would open that file and export only the "Hello" string to a new file which is say "C:\output.txt" I am sure it is something easy that I am just overlooking but I am trying to design a log analyzer program and I can't seem to get started. Thanks in advance.
|
Wed, 18 Jun 2003 11:30:16 GMT |
|
 |
Larry Linso #2 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Start by reading up on VB's File I/O, specifically for text files, the Open Statement, the Input # and Line Input statements, and the Close statement, also the EOF function, and the Print # functions. You don't export you Print to that text file... may seem strange (it did to me at first, because I always thought of "writing" to a file, not "printing" to a file) but that's the way text files work.
Quote: > I need a little help. I have been searching and searching for an answer to > this and have come up with zilch. > If I have a file called "C:\test.txt" and the files consists of the > following: > ------ > Goodbye > Hello > Next Year > ------ > How you would open that file and export only the "Hello" string to a new > file which is say "C:\output.txt" > I am sure it is something easy that I am just overlooking but I am trying to > design a log analyzer program and I can't seem to get started. > Thanks in advance.
-- L. M. (Larry) Linson http://www.ntpcug.org - North Texas PC User Group - Visit and Join http://www.ntmsdevsigs.homestead.com - NTPCUG Developer SIGs http://homestead.deja.com/user.accdevel - Access examples Sent via Deja.com http://www.deja.com/
|
Wed, 18 Jun 2003 13:10:37 GMT |
|
 |
adpx #3 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Kevin, If the log analyzer is for your use only, consider using Perl. Really. Perl excels at text parsing, and with the correct invocation the code to do what you need reduces to: print if /Hello/; --A
|
Wed, 18 Jun 2003 14:38:55 GMT |
|
 |
Rick Rothstei #4 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
I think you need to provide just a little more information. Obviously, you are choosing the "Hello" based on something other than its text (or you could simply write "Hello" directly to the output file without ever reading the input file). What criteria are you using to get the "Hello" string? The fact that it is the 2nd line in the file? That is starts with the letter "H"? That it is the next to the last line in the file? Or??? Rick
Quote: > I need a little help. I have been searching and searching for an answer to > this and have come up with zilch. > If I have a file called "C:\test.txt" and the files consists of the > following: > ------ > Goodbye > Hello > Next Year > ------ > How you would open that file and export only the "Hello" string to a new > file which is say "C:\output.txt" > I am sure it is something easy that I am just overlooking but I am trying to > design a log analyzer program and I can't seem to get started. > Thanks in advance.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World! -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
|
Wed, 18 Jun 2003 16:04:53 GMT |
|
 |
hmck #5 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Kevin Try the following. A bit old fashioned but it should work. Open "c:\test.txt" For Input As #1 Open "c:\output.txt" for Ouput as #2 100 If EOF(1) Then GoTo 300 Input #1, e if e = "Hello" then goto 200 else goto 100 200 Print #2, e goto 100: rem just in case there is more than one hello 300 Close Hugh McKerrell
On Sat, 30 Dec 2000 03:30:16 GMT, "Kevin Wells" Quote:
>I need a little help. I have been searching and searching for an answer to >this and have come up with zilch. >If I have a file called "C:\test.txt" and the files consists of the >following: >------ >Goodbye >Hello >Next Year >------ >How you would open that file and export only the "Hello" string to a new >file which is say "C:\output.txt" >I am sure it is something easy that I am just overlooking but I am trying to >design a log analyzer program and I can't seem to get started. >Thanks in advance.
|
Wed, 18 Jun 2003 19:25:20 GMT |
|
 |
Steve [ethno #6 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Quote: > Try the following. A bit old fashioned but it should work.
NO! DO NOT TRY THE ABOVE CODE! IT CONTAIN'S GOTO! AAAAAAAARGH! Sorry to shout, but GoTos are the WORST possible thing to use when you are trying to write structured code, because they make program flow entirely unpredictable. If you must use Visual Basic for this task, then do it this way - dim sIn as string dim sOut as string dim sTest as string sIn = "c:\test.txt" sOut = "c:\output.txt" open sIn for Input as #1 open sOut for Output as #2 do while not eof(1) sTest = Line Input #1 if sTest like "hello" then print #2, sTest end if DoEvents loop close #2 close #1 This, I am sure you will agree, is FAR more elegant and FAR more predictable in its effects; it also has the advantage of being clear and easy to read. steve
|
Wed, 18 Jun 2003 20:35:54 GMT |
|
 |
Michael William #7 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Quote: > NO! DO NOT TRY THE ABOVE CODE! IT CONTAIN'S GOTO! AAAAAAAARGH!
If we never used any code that contained GOTO then we would not be able to use any code that contained error trapping! Mike
|
Wed, 18 Jun 2003 21:17:24 GMT |
|
 |
Kevin Well #8 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Thanks Steve, Here is a question for you. If you take the same example but had a text file like the following: User Logged on at: 11:24 User Transferred 10 Files User Logged off at 11:36 User Logged on at 12:45 User Logged off at 01:45 Using the below example as a reference, is there anyway to isolate only the "User Logged on at: ??:??" lines to the output file? I have tried using the filter function to no avail as it keeps giving me the "Type Mismatch" error when using the following code: ------------------- Private Sub Command2_Click() Dim str As String Dim search As String Open "c:\test.log" For Input As #1 Open "c:\textfinish.log" For Output As #2 search = "FTP" Do Until EOF(1) Line Input #1, str If Filter(str, search) Then Print #2, str End If Loop -------------------- If anyone else knows the answer then please share. Thanks
Quote: > > Try the following. A bit old fashioned but it should work. > NO! DO NOT TRY THE ABOVE CODE! IT CONTAIN'S GOTO! AAAAAAAARGH! > Sorry to shout, but GoTos are the WORST possible thing to use when you are > trying to write structured code, because they make program flow entirely > unpredictable. > If you must use Visual Basic for this task, then do it this way - > dim sIn as string > dim sOut as string > dim sTest as string > sIn = "c:\test.txt" > sOut = "c:\output.txt" > open sIn for Input as #1 > open sOut for Output as #2 > do while not eof(1) > sTest = Line Input #1 > if sTest like "hello" then > print #2, sTest > end if > DoEvents > loop > close #2 > close #1 > This, I am sure you will agree, is FAR more elegant and FAR more predictable > in its effects; it also has the advantage of being clear and easy to read. > steve
|
Wed, 18 Jun 2003 21:23:05 GMT |
|
 |
Michael William #9 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
You are using the wrong function to check your string. The VB Filter function that you are using is for checking an array of strings and it returns another array that contains a subset of the first array according to the search criteria. The function that you need to use in your case is the VB Instr function. This searches a string to see if a specified substring exists within it. Try the following: Private Sub Command1_Click() Dim str As String Dim search As String Open "c:\test.log" For Input As #1 Open "c:\textfinish.log" For Output As #2 search = "User logged on" Do Until EOF(1) Line Input #1, str If InStr(1, str, search, vbTextCompare) > 0 Then Print #2, str End If Loop Close 1 Close 2 End Sub Mike
Quote: > Thanks Steve, > Here is a question for you. If you take the same example but had a text > file like the following: > User Logged on at: 11:24 > User Transferred 10 Files > User Logged off at 11:36 > User Logged on at 12:45 > User Logged off at 01:45 > Using the below example as a reference, is there anyway to isolate only the > "User Logged on at: ??:??" lines to the output file? I have tried using the > filter function to no avail as it keeps giving me the "Type Mismatch" error > when using the following code: > ------------------- > Private Sub Command2_Click() > Dim str As String > Dim search As String > Open "c:\test.log" For Input As #1 > Open "c:\textfinish.log" For Output As #2 > search = "FTP" > Do Until EOF(1) > Line Input #1, str > If Filter(str, search) Then > Print #2, str > End If > Loop > -------------------- > If anyone else knows the answer then please share. > Thanks
> > > Try the following. A bit old fashioned but it should work. > > NO! DO NOT TRY THE ABOVE CODE! IT CONTAIN'S GOTO! AAAAAAAARGH! > > Sorry to shout, but GoTos are the WORST possible thing to use when you are > > trying to write structured code, because they make program flow entirely > > unpredictable. > > If you must use Visual Basic for this task, then do it this way - > > dim sIn as string > > dim sOut as string > > dim sTest as string > > sIn = "c:\test.txt" > > sOut = "c:\output.txt" > > open sIn for Input as #1 > > open sOut for Output as #2 > > do while not eof(1) > > sTest = Line Input #1 > > if sTest like "hello" then > > print #2, sTest > > end if > > DoEvents > > loop > > close #2 > > close #1 > > This, I am sure you will agree, is FAR more elegant and FAR more > predictable > > in its effects; it also has the advantage of being clear and easy to read. > > steve
|
Wed, 18 Jun 2003 22:24:34 GMT |
|
 |
Steve [ethno #10 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Quote: > If we never used any code that contained GOTO then we would not be able to > use any code that contained error trapping!
If you can justify the programmatical use of GoTo in a structured programming exercise, then I'll use it. Error handling is an exception, but is still horribly implemented in VB6. I'm playing with VB.NET at the moment... Try, Catch, Throw... mmm exception handling makes VB programming a LOT nicer, and obviates the need for gotos. steve
|
Thu, 19 Jun 2003 00:58:10 GMT |
|
 |
Steve [ethno #11 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Try using either the inStr function, or, for slightly more flexibility, look into the Like operator - it let's you use wildcards and ranges to achieve the correct effect. So, if you replaced this : If Filter(str, search) Then with If search like str then it would work well. you would also have to change the 'search' variable to me something like 'user logged on at*' in order that it matched all lines like that. HTH steve
|
Thu, 19 Jun 2003 01:04:22 GMT |
|
 |
rlwals #12 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Hi, Spend some time studying the help files until you understand each of the File I/O statements used below: Dim iFb1 As Integer, iFB2 As Integer, sTmp As String iFB = FreeFile Open "C:\Test.txt" For Input As #iFB Do While Not EOF(iFB) Line Input #1, sTmp if sTmp = "Hello" Then 'Or: If Instr(sTmp, "Hello") if you prefer. Open "C:\Output.txt" For Output As iFB2 Print #iFB2, sTmp Close iFB2 Exit Do End if Loop Close iFB1 -------------------------
Quote: > I need a little help. I have been searching and searching for an answer to > this and have come up with zilch. > If I have a file called "C:\test.txt" and the files consists of the > following: > ------ > Goodbye > Hello > Next Year > ------ > How you would open that file and export only the "Hello" string to a new > file which is say "C:\output.txt" > I am sure it is something easy that I am just overlooking but I am trying to > design a log analyzer program and I can't seem to get started. > Thanks in advance.
|
Thu, 19 Jun 2003 01:08:15 GMT |
|
 |
Michael William #13 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Quote: > > If we never used any code that contained GOTO then we would not be able to > > use any code that contained error trapping! > I'm playing with VB.NET at the moment... Try, Catch, Throw... mmm exception > handling makes VB programming a LOT nicer, and obviates the need for
gotos. I'm glad that Visual Basic is soon about to catch up with the twenty year old Commodore Amiga ;-) Mike
|
Thu, 19 Jun 2003 01:29:21 GMT |
|
 |
hmck #14 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
Steve I really find your kind of code as strange as I am sure you find mine. There are no rules for doing this kind of thing and if it works, why fix it? How on earth there can be a right and a wrong way of using vb I totally fail to see. If it works, it works. I have written basic code long before vb appeared so gotos are entirely routine to me. I can honestly say I never get confused with them and for the life of me I cannot see why you should get so het up. One detail I note, my code size is about half yours. Does that not tell you something? Like pedantic for the sake of it? Anyway, I have no intention of getting involved in any dispute. But I just thought I should respond to rather totally reject your suggestions. I wont complain about your kind of thing if you dont complain about mine. Hugh McKerrell
On Sat, 30 Dec 2000 13:17:24 -0000, "Michael Williams" Quote:
>> NO! DO NOT TRY THE ABOVE CODE! IT CONTAIN'S GOTO! AAAAAAAARGH! >If we never used any code that contained GOTO then we would not be able to >use any code that contained error trapping! >Mike
|
Thu, 19 Jun 2003 01:30:37 GMT |
|
 |
Michael William #15 / 25
|
 Working with Text Files - Searching for strings and then exporting to new file
The Like operator is good, but you have to watch out for the fact that it uses the Option Compare setting of your module (which defaults to Binary, making it case sensitive). For example, your own example of 'user logged on at*' would not work in Kevin's case. Mike
Quote: > Try using either the inStr function, or, for slightly more flexibility, look > into the Like operator - it let's you use wildcards and ranges to achieve > the correct effect. > So, if you replaced this : > If Filter(str, search) Then > with > If search like str then > it would work well. > you would also have to change the 'search' variable to me something like > 'user logged on at*' in order that it matched all lines like that. > HTH > steve
|
Thu, 19 Jun 2003 01:35:13 GMT |
|
|