Results 1 to 12 of 12

Thread: please help.. important

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    302

    please help.. important

    Hi,

    I have a text file that I want to edit...

    I want to deleteall empty lines from it.

    I can open the file and rewrite the content (without the empty lines) to another file... but the code looks messy and SLOW!

    Can you please help ?

    Some lines are totally empty, but others have spaces... i want to delete all the lines that don't have text..


    Thanks

  2. #2
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    I may be wrong, but i dont think theres any way of doing this without running through the whole file. You can test the Trim function with the vbCrLf character to see it is returns a "". if so, then you can test for it and then either write or not write the file back. I hope i made some sense. oh btw, the Trim function gets trim function gets rid of leading and trailing spaces.
    You just proved that sig advertisements work.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    302
    how can i check if the line has a space or a return "enter" ?

  4. #4
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    You are going to have to open the file. Then... since you are using VB 6, you can use the replace function to change all trim(vbCrLf)'s to "". It should work, I think.
    <removed by admin>

  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Just use InStr() if you want to check for those things. Checking for a space won't be that helpful though since it won't tell you if it's a legitimate space or a series of running ones. You could make a function to count spaces in lines and then ignore the line if you've got a bunch, but you could still get the same problem there. You shouldn't use Replace() like that though or you'll completely lose real lines, and have all your text jumbled together.

    A better way would be to read the file in a single shot, use Split() on that to break it into lines, and then give that array to Filter() so you can exclude the true blank lines, which would be "" inside the split array. You could also do things like excluding lines with 6 or 7 spaces in a row or something. Then just use Join() on that array again to get a string, and rewrite it back to a file. It sounds like a lot, but there's really not that much to it. Leaving it up to the functions like this is a lot easier than making your own checking on it to look for the space repeats and blank lines. Take a look at this:

    http://www.vbforums.com/showthread.p...highlight=text
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  6. #6
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    This is a neat, simple way of doing it. I tested it and it seems to work fine. Hope it helps!

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Call RemoveBlankLines("c:\test.txt", "c:\test2.txt")
    5. End Sub
    6.  
    7. Public Sub RemoveBlankLines(SourceFileName As String, DestinationFileName As String)
    8. Dim strfile As String, strsearch As String
    9. Dim FF As Integer
    10. FF = FreeFile
    11. Open SourceFileName For Binary As #FF
    12. strfile = Space$(LOF(1))
    13. Get #FF, 1, strfile
    14. Close #FF
    15. strsearch = vbCrLf & vbCrLf
    16. strfile = Replace(strfile, strsearch, "")
    17. Open DestinationFileName For Binary As #FF
    18. Put #FF, 1, strfile
    19. Close #FF
    20. End Sub

  7. #7
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    hmm, didnt catch the part about the spaces. my last post wont work

  8. #8
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    the following is the cleanest thing I could come up with. It works.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Call RemoveBlankLines("c:\test.txt", "c:\test2.txt")
    5. End Sub
    6.  
    7. Public Sub RemoveBlankLines(SourceFileName As String, DestinationFileName As String)
    8. Dim strfile As String, strsearch As String
    9. Dim FF As Integer, test As Integer
    10. FF = FreeFile
    11. Open SourceFileName For Binary As #FF
    12. strfile = Space$(LOF(1))
    13. Get #FF, 1, strfile
    14. Close #FF
    15. strsearch = vbCrLf & " "
    16. test = 1
    17. Do Until test = 0
    18.     strfile = Replace(strfile, strsearch, vbCrLf)
    19.     test = InStr(strfile, strsearch)
    20. Loop
    21. strsearch = vbCrLf & vbCrLf
    22. test = 1
    23. Do Until test = 0
    24.     strfile = Replace(strfile, strsearch, vbCrLf, , 1)
    25.     test = InStr(strfile, strsearch)
    26. Loop
    27. Open DestinationFileName For Output As #FF
    28. Print #FF, strfile
    29. Close #FF
    30. End Sub

  9. #9
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Thumbs up Try this...

    VB Code:
    1. Option Explicit
    2. Dim OLD As String, NN As String, FREO As String
    3. Private Sub Form_Load()
    4. FREO = FreeFile
    5. CLEAN ("c:\xxx.txt")
    6. End Sub
    7. Function CLEAN(file As String)
    8. Open file For Input As #FREO
    9. While Not EOF(FREO)
    10. Line Input #FREO, OLD
    11. NN = NN & OLD
    12. Wend
    13. Close #FREO
    14. Open file For Output As #FREO
    15. Print #FREO, NN
    16. Close #FREO
    17. Unload Me
    18. End Function
    This code opens up c:\xxx.txt and cleans it.

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  10. #10
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    prog_tom, that doesn't "clean" up a file. That will end up removing any semblance of lines completely, and turn the while file into a huge running one. I hope you tested that out before you pasted, because it's not a good example...
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  11. #11
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Cool THe code

    Well, isn't that what he wants??? Also, muddy, your code only copies, doesn't removes spaces,...

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  12. #12
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: THe code

    Originally posted by prog_tom
    Well, isn't that what he wants??? Also, muddy, your code only copies, doesn't removes spaces,...
    my code removes lines with nothing but spaces and lines with nothing at all (my latest post anyway).. give it a try.

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