|
-
Jul 9th, 2006, 12:27 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Read File: Search for text if found Delete Line
Hello Guys,
Need a bit help here...
I need to be able to read a file and search for a certain word.
If it exists, then delete whole line.
Ive tried it with the FSO method, but keep on getting 'System Variable with Block variable not set'
Is there another way to do this with the vb method of OPEN fNum ?
Thanx in advance
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 9th, 2006, 12:52 PM
#2
Re: Read File: Search for text if found Delete Line
Without seeing how you did it it's difficult to determine which object isn't set...
Anyway, general idea is to re-write the file by omitting certain lines. Here is a quick sample for you (untested):
VB Code:
Private Sub Command1_Click()
Dim sFile$, sText$, arLines() As String
Dim i%, sKeyWord$
sFile = "c:\test.txt"
Open sFile For Input As #1
'read entire file into a string varaiable
sText = Input(LOF(1), #1)
Close #1
'assign some word you wish to exclude from the file
sKeyWord = "whatever"
'split text into array
arLines = Split(sText, vbNewLine)
Open sFile For Output As #1
For i = 0 To UBound(arLines)
'check if key word is part of the string
If InStr(1, arLines(i), sKeyWord) = 0 Then
'it is not so print it
Print #1, arLines(i)
End If
Next i
Close #1
End Sub
-
Jul 9th, 2006, 12:53 PM
#3
Re: Read File: Search for text if found Delete Line
-
Jul 9th, 2006, 04:16 PM
#4
Thread Starter
Frenzied Member
Re: Read File: Search for text if found Delete Line
Thank you there guys..
However i found out that this actually clears even if another contains a small part of your defined string to delete...
for example:
I want to delete:
user.name.history
But if the file also contains something like:
visited history was on ... this line gets deleted too!!
How can i prevent this?
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 9th, 2006, 04:21 PM
#5
Re: Read File: Search for text if found Delete Line
From the example you gave,
your search string is "history" right.
Then it will delete both the lines. if you want to delete the line "user.name.history" then your search string may/should be "user.name.history". it will delete only one line.
-
Jul 9th, 2006, 04:31 PM
#6
Thread Starter
Frenzied Member
Re: Read File: Search for text if found Delete Line
aahha.. my fault:
What happened was I did a check in MS Word for a Line Count:
1st result gave me 14: (no modification original file)
after i ran the function i got: 12
So i thought it deleted multiple instances, but what it was actually doing was
It puts an empty line on top of the file when it rewrites it: Dont know why:
But sorted now anyway... thanx
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 9th, 2006, 04:34 PM
#7
Re: [RESOLVED] Read File: Search for text if found Delete Line
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
|