|
-
Aug 13th, 2001, 05:13 PM
#1
Thread Starter
Hyperactive Member
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
-
Aug 13th, 2001, 05:17 PM
#2
Frenzied Member
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.
-
Aug 13th, 2001, 05:49 PM
#3
Thread Starter
Hyperactive Member
how can i check if the line has a space or a return "enter" ?
-
Aug 13th, 2001, 05:58 PM
#4
PowerPoster
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.
-
Aug 13th, 2001, 06:20 PM
#5
Fanatic Member
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 
-
Aug 13th, 2001, 07:12 PM
#6
PowerPoster
This is a neat, simple way of doing it. I tested it and it seems to work fine. Hope it helps!
VB Code:
Option Explicit
Private Sub Command1_Click()
Call RemoveBlankLines("c:\test.txt", "c:\test2.txt")
End Sub
Public Sub RemoveBlankLines(SourceFileName As String, DestinationFileName As String)
Dim strfile As String, strsearch As String
Dim FF As Integer
FF = FreeFile
Open SourceFileName For Binary As #FF
strfile = Space$(LOF(1))
Get #FF, 1, strfile
Close #FF
strsearch = vbCrLf & vbCrLf
strfile = Replace(strfile, strsearch, "")
Open DestinationFileName For Binary As #FF
Put #FF, 1, strfile
Close #FF
End Sub
-
Aug 13th, 2001, 07:20 PM
#7
PowerPoster
hmm, didnt catch the part about the spaces. my last post wont work
-
Aug 13th, 2001, 07:44 PM
#8
PowerPoster
the following is the cleanest thing I could come up with. It works.
VB Code:
Option Explicit
Private Sub Command1_Click()
Call RemoveBlankLines("c:\test.txt", "c:\test2.txt")
End Sub
Public Sub RemoveBlankLines(SourceFileName As String, DestinationFileName As String)
Dim strfile As String, strsearch As String
Dim FF As Integer, test As Integer
FF = FreeFile
Open SourceFileName For Binary As #FF
strfile = Space$(LOF(1))
Get #FF, 1, strfile
Close #FF
strsearch = vbCrLf & " "
test = 1
Do Until test = 0
strfile = Replace(strfile, strsearch, vbCrLf)
test = InStr(strfile, strsearch)
Loop
strsearch = vbCrLf & vbCrLf
test = 1
Do Until test = 0
strfile = Replace(strfile, strsearch, vbCrLf, , 1)
test = InStr(strfile, strsearch)
Loop
Open DestinationFileName For Output As #FF
Print #FF, strfile
Close #FF
End Sub
-
Aug 13th, 2001, 11:42 PM
#9
Fanatic Member
Try this...
VB Code:
Option Explicit
Dim OLD As String, NN As String, FREO As String
Private Sub Form_Load()
FREO = FreeFile
CLEAN ("c:\xxx.txt")
End Sub
Function CLEAN(file As String)
Open file For Input As #FREO
While Not EOF(FREO)
Line Input #FREO, OLD
NN = NN & OLD
Wend
Close #FREO
Open file For Output As #FREO
Print #FREO, NN
Close #FREO
Unload Me
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
-
Aug 14th, 2001, 04:20 AM
#10
Fanatic Member
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 
-
Aug 14th, 2001, 05:22 AM
#11
Fanatic Member
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
-
Aug 14th, 2001, 05:50 AM
#12
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|