By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,924 Members | 565 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,924 IT Pros & Developers. It's quick & easy.

text filtering with Visual Basic

P: 3

I have some problems regarding text filtering with Visual Basic.
In order to filter only some text lines I want to make a copy of the file as a temporary one, read this copy as a file input via basic and print out to the file.
The original text file looks as follows:
2.11.2007 11:35:36 The new source 'SOCKET:' has been accepted
22.11.2007 11:34:42 Network Services stopped (GT EX TEST)
22.11.2007 11:34:52 Network Services started (GT EX TEST)
There are hundreds of these messages. This text file is log file of a software.
In a copied text file I only want to copy the text lines (system messages) which I need to see.

I will be really grateful if you give me some tips for the solution of the problem.
Nov 26 '07 #1
Share this Question
Share on Google+
8 Replies

Expert 100+
P: 1,445

Open the source file, and another text file, and copy line by line with mod function.
Mid(TempString,20) gives you the message.

Nov 26 '07 #2

Expert 5K+
P: 8,434
You really haven't made it clear which part of the process is giving you problems, which lines you want and don't want, or what version of VB you're using.

Veena, assuming that stripping off the date & time is what was required, I don't think a straight Mid() function like this will work properly because the length of the date varies. Possibly the time also, but we can't tell from this sample. You'd really need to do something like using Instr() to find the second space.
Nov 26 '07 #3

P: 3
Thanks for your feedbacks...
so i am using Visual Basic 6.0...
i am working on a software (SCADA) which writes automaticlly his system messages on his own log file... I only need to write a small program in VB 6.0 which reads all these system messages from this log file and write the selected lines (messages ) into another text file....
The filtering could be done with key words (like Socket:)
If the keyword can be found in the line, this line should be copied in the other text file..

Best Regards
Nov 26 '07 #4

Expert 5K+
P: 8,434
That kind of filtering is rpetty simple, then. You just do a test like...
Expand|Select|Wrap|Line Numbers
  1. If Instr(LineReadFromFile1, MyKeyWord) Then
  2.   Print #2, LineReadFromFile1
  3. End If
Nov 27 '07 #5

P: 3
Here is my code.. But i can not copy the selected lines from test to test1
Have someone any idea?

Sub Main

Dim data As String
FileCopy("C:\Documents and Settings\OTOMASYON\Belgelerim\Movicon Projects\GT EX TEST\LOGS\Network Services.log", "C:\test.txt")

Open "C:\test.txt" For Input As #1
Open "C:\test1.txt" For Append As #2

Do While Not EOF (1)
Line Input #1, data
If InStr(data, "Network") Then
Print #2, data
End If

Close #1
Close #2

End Sub
Nov 30 '07 #6

Expert 100+
P: 1,445

"InStr" Function Returns an Integer Value, if the String is Found, Change your If Condition :

Expand|Select|Wrap|Line Numbers
  1. Sub Main
  2. Dim data As String
  3. FileCopy("C:\Documents and Settings\OTOMASYON\Belgelerim\Movicon Projects\GT EX TEST\LOGS\Network Services.log", "C:\test.txt")
  5. Open "C:\test.txt" For Input As #1
  6. Open "C:\test1.txt" For Append As #2
  8. Do While Not EOF (1)
  9.     Line Input #1, data
  10.     If InStr(data, "Network") >0 Then
  11.         Print #2, data
  12.     End If
  13. Loop
  15. Close #1
  16. Close #2
  18. End Sub
Not very sure, why you are writing in Sub Main.
You can write a General Procedure with some name Like (ImportLog) and Call the Proc wherever you want..

Nov 30 '07 #7

Expert 5K+
P: 8,434
I'm surprised it didn't work.

Veena, VB effectively treats any non-zero value as True, and zero as False, in this sort of circumstance. So while the > 0 does make the code a little more obvious to a human reading it, it's actually unnecessary.
Dec 9 '07 #8

Expert 5K+
P: 8,434
Hey levi, has any of this helped at all?
Dec 12 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.