Results 1 to 7 of 7

Thread: [RESOLVED] Read File: Search for text if found Delete Line

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Resolved [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.



  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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:
    1. Private Sub Command1_Click()
    2. Dim sFile$, sText$, arLines() As String
    3. Dim i%, sKeyWord$
    4.  
    5.     sFile = "c:\test.txt"
    6.     Open sFile For Input As #1
    7.         'read entire file into a string varaiable
    8.         sText = Input(LOF(1), #1)
    9.     Close #1
    10.    
    11.     'assign some word you wish to exclude from the file
    12.     sKeyWord = "whatever"
    13.    
    14.     'split text into array
    15.     arLines = Split(sText, vbNewLine)
    16.    
    17.     Open sFile For Output As #1
    18.         For i = 0 To UBound(arLines)
    19.             'check if key word is part of the string
    20.             If InStr(1, arLines(i), sKeyWord) = 0 Then
    21.                 'it is not so print it
    22.                 Print #1, arLines(i)
    23.             End If
    24.         Next i
    25.     Close #1
    26.  
    27. End Sub

  3. #3
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: Read File: Search for text if found Delete Line

    CS

  4. #4

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    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.



  5. #5
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    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.
    CS

  6. #6

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    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.



  7. #7
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: [RESOLVED] Read File: Search for text if found Delete Line

    Great.

    You are Welcome.
    CS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width