Results 1 to 9 of 9

Thread: Does Anyone know?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Does Anyone know?

    How can I delete a line from a textfile via code?

  2. #2
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    The simplest way is using Replace function by replace those line with "" (Nothing)

    OR

    open a new file by copy the first half (from begining till the starting point of the line you which to replace) and follow by copy the second half (from the end of the line which you wish to remove till the end of the file).

    regards,

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    ok.......

    Say I found the word "BOB" in the fifth line of my textfile. Now I want to get rid of that line. How would I do that using the replace function? Would I have to seek to that line or something?

  4. #4
    Hyperactive Member Blinky Bill's Avatar
    Join Date
    Mar 2002
    Location
    Happily munching on the greenery in your garden
    Posts
    349
    You need to open the file, read all the lines into an array or something, find what you want to remove, remove it from the array, and then write the contents of the array back to the file.
    We don't know what's wrong. . . So the best bet might be to remove something surgically.

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    inthis case, i suggest to use the Regular Expression Check out the sample at RegExp

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    An array like this?

    VB Code:
    1. Dim tmp as string
    2. Dim temp () as string
    3.  
    4. temp () = Open "c:\windows\system\" & sDirname For Input As #1
    5.                       Do Until EOF(1)
    6.                       Line Input #1, tmp
    7.                    Loop
    8.                 Close #1

    I'm kind of confused on this. How would I go about searching in an array for a string? Once I found it, I would just remove the # assigned to it in the array right?

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    whatz do you wish to search from the "c:\windows\system\" & sDirname?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Talking oh!!

    I just posted a piece of the code. That sDirname is a string that h as the folder name stored that it gets from a dirlistbox. Hows this:

    Open "c:\windows\system\modernrock.txt" for input as #1

  9. #9
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217

    Well...

    VB Code:
    1. dim a() as string
    2. open "C:\something.txt" for input as #1
    3. a = split(Input$(LOF(1),#1),vbcrlf) 'reads the whole file and splits lines to an array
    4. close #1
    5.  
    6. open "C:\something.txt" for output as #1
    7. for i = lbound(a) to ubound(a)
    8. if a(i) <> "BOB" then 'do your searching here
    9. print #1,a(i) 'if the line is not BOB then put it in the file (if it IS bob, then it doesn't
    10. end if
    11. next
    12. close #1
    Remember, if someone's post was not helpful, you can always rate their post negatively .

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