dokken
asked on
removing return codes from a file
I have a program that is grabbing certain lines from a file and I want to remove the returns before it re-writes it to another file. That way the line it writes in only one line instead of several. Is there a quick and easy way to do this?
haven't tried that, exactly, but, doesn't VB convert the carriage returns into string terminators?
are you getting multiple carriage return/line feed pairs in your destination file?
are you getting multiple carriage return/line feed pairs in your destination file?
ASKER
I am getting returns were there were returns in the file it's reading. I would like to remove the returns and just store what I read from the file all on one line.
you are reading the file in as text, correct?
you want it in one line. you mean one string?
like this:
This
Is
Many
Lines
to be:
ThisIsManyLines
right?
try binary read only access. read the file in one byte at a time. have a condition in there, that if you hit a char 13, then check to see if the next one is a char 10, if it is, dump them. while you are iterating through the file, you concatenate the bytes into a string.... have some code somewhere that did this from a file being transferred via com port. could look it up.
you want it in one line. you mean one string?
like this:
This
Is
Many
Lines
to be:
ThisIsManyLines
right?
try binary read only access. read the file in one byte at a time. have a condition in there, that if you hit a char 13, then check to see if the next one is a char 10, if it is, dump them. while you are iterating through the file, you concatenate the bytes into a string.... have some code somewhere that did this from a file being transferred via com port. could look it up.
ASKER
Thats exactly what I want...
to make this:
This
Is
Many
Lines
turn in to this:
ThisIsManyLines
I'll try your suggestion. If you still have that code I'd like to see it.
to make this:
This
Is
Many
Lines
turn in to this:
ThisIsManyLines
I'll try your suggestion. If you still have that code I'd like to see it.
sure. hold on to your horses.... I was just in there Wednesday. Hate that client.... :) It's similar, lemme look at it for a coupla.
ASKER
Thats exactly what I want...
to make this:
This
Is
Many
Lines
turn in to this:
ThisIsManyLines
I'll try your suggestion. If you still have that code I'd like to see it.
to make this:
This
Is
Many
Lines
turn in to this:
ThisIsManyLines
I'll try your suggestion. If you still have that code I'd like to see it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops! hit the wrong thingie. <sg>
you might have a problem pasting it in, this interface is too narrow. just makes sure that all the lines go in correctly.
The code's not as pretty as it could be, but it works... :)
Created a file called "Label.Txt" on C:\
Had our test:
This
Is
Many
Lines
Sprang up a message box of "ThisIsManyLines". Had to hack-out non-pertinent code.
Any how. Make that file in your root. open a vb project and dump this code into the form
-------------------------- ---------- ---------- ---------- -
'
'please excuse the mess, I am the 4th person or so to touch this 'code
'and wanted to do a re-write. that way everything would be done 'the
'correct way, that is to say, my way. :)
'
Option Explicit
Dim clear As Boolean
Dim bytInputStream() As Byte ' byte stream from file
Dim intInputLength() As Integer ' keeps track of line length
Dim lngIndex As Long ' count of element in array
Dim intRows As Long
Private Sub Capture()
Dim Char As String * 1
Dim LastChar As String * 1
Dim LineLength As Integer
Dim File As String
ChDir "C:\"
File = Dir("label.txt")
DoEvents
If (File = "") Then Exit Sub
lngIndex = 0
intRows = 0
ReDim bytInputStream(lngIndex)
ReDim intInputLength(intRows)
' tmrCapture.Enabled = False
Open "label.txt" For Binary Access Read As #1
Do While (Not EOF(1))
Get #1, , Char
If (EOF(1)) Then Exit Do
bytInputStream(lngIndex) = Asc(Char)
If (LastChar = Chr(13) And Char = Chr(10)) Then
intInputLength(intRows) = LineLength + 1
intRows = intRows + 1
ReDim Preserve intInputLength(intRows)
LineLength = 0
Else
LineLength = LineLength + 1
End If
lngIndex = lngIndex + 1
ReDim Preserve bytInputStream(lngIndex)
LastChar = Char
Loop
Close #1
'this does it! Tried it me-self. :)
MsgBox FormStr(0, 0, lngIndex)
Exit Sub
errCapture:
MsgBox Err.Number & " " & Err.Description
End Sub
'
'this routine turns the array of bytes back into a string
'and allows you to access certain elements by Row, Position and
'length
'
Public Function FormStr(Row As Integer, Position As Long, Length As Long) As String
Dim A As Integer
Dim Index As Integer
Dim S As String
Index = FindIndex(Row, Position)
'this makes sure that only valid ascii characters are concatenated
'into the string.
For A = Index To Index + Length - 1
If (bytInputStream(A) > 31 And bytInputStream(A) < 128) Then
S = S & Chr(bytInputStream(A))
End If
Next A
FormStr = S
End Function
'
'this routine let's you position yourself in the 1 dimensional 'array
'as though it were an array of variable length strings
'
Private Function FindIndex(Row As Integer, Position As Long) As Long
Dim A As Integer
Dim Index As Long
For A = 0 To Row - 1
Index = Index + intInputLength(A)
Next A
Index = Index + Position
FindIndex = Index
End Function
Private Sub Form_Load()
Capture
End Sub
you might have a problem pasting it in, this interface is too narrow. just makes sure that all the lines go in correctly.
The code's not as pretty as it could be, but it works... :)
Created a file called "Label.Txt" on C:\
Had our test:
This
Is
Many
Lines
Sprang up a message box of "ThisIsManyLines". Had to hack-out non-pertinent code.
Any how. Make that file in your root. open a vb project and dump this code into the form
--------------------------
'
'please excuse the mess, I am the 4th person or so to touch this 'code
'and wanted to do a re-write. that way everything would be done 'the
'correct way, that is to say, my way. :)
'
Option Explicit
Dim clear As Boolean
Dim bytInputStream() As Byte ' byte stream from file
Dim intInputLength() As Integer ' keeps track of line length
Dim lngIndex As Long ' count of element in array
Dim intRows As Long
Private Sub Capture()
Dim Char As String * 1
Dim LastChar As String * 1
Dim LineLength As Integer
Dim File As String
ChDir "C:\"
File = Dir("label.txt")
DoEvents
If (File = "") Then Exit Sub
lngIndex = 0
intRows = 0
ReDim bytInputStream(lngIndex)
ReDim intInputLength(intRows)
' tmrCapture.Enabled = False
Open "label.txt" For Binary Access Read As #1
Do While (Not EOF(1))
Get #1, , Char
If (EOF(1)) Then Exit Do
bytInputStream(lngIndex) = Asc(Char)
If (LastChar = Chr(13) And Char = Chr(10)) Then
intInputLength(intRows) = LineLength + 1
intRows = intRows + 1
ReDim Preserve intInputLength(intRows)
LineLength = 0
Else
LineLength = LineLength + 1
End If
lngIndex = lngIndex + 1
ReDim Preserve bytInputStream(lngIndex)
LastChar = Char
Loop
Close #1
'this does it! Tried it me-self. :)
MsgBox FormStr(0, 0, lngIndex)
Exit Sub
errCapture:
MsgBox Err.Number & " " & Err.Description
End Sub
'
'this routine turns the array of bytes back into a string
'and allows you to access certain elements by Row, Position and
'length
'
Public Function FormStr(Row As Integer, Position As Long, Length As Long) As String
Dim A As Integer
Dim Index As Integer
Dim S As String
Index = FindIndex(Row, Position)
'this makes sure that only valid ascii characters are concatenated
'into the string.
For A = Index To Index + Length - 1
If (bytInputStream(A) > 31 And bytInputStream(A) < 128) Then
S = S & Chr(bytInputStream(A))
End If
Next A
FormStr = S
End Function
'
'this routine let's you position yourself in the 1 dimensional 'array
'as though it were an array of variable length strings
'
Private Function FindIndex(Row As Integer, Position As Long) As Long
Dim A As Integer
Dim Index As Long
For A = 0 To Row - 1
Index = Index + intInputLength(A)
Next A
Index = Index + Position
FindIndex = Index
End Function
Private Sub Form_Load()
Capture
End Sub
ASKER
I tried that, real close to what I want... just one more thing, is there a way to seperate the sentences that it pulled into one string by a space? I'll even jack up the points if you can also tell my how to strip spaces in case there is more then 1.
could you give me an example of what you would like?
ASKER
sure, I'm working on a project that imports meta tags from html files. some of the meta tags have returns in the lines... like someone did this:
<meta name="Description" content="this all
should have
been on one
line">
so I want to remove any extra spaces and convert that to one line.
<meta name="Description" content="this all
should have
been on one
line">
so I want to remove any extra spaces and convert that to one line.
what would the destination text look like?
<meta name="Description" content = "thisallshouldhavebeenonon eline">?
not:
<metaname="Description"con tent="this allshouldh avebeenono neline">?
???
<meta name="Description" content = "thisallshouldhavebeenonon
not:
<metaname="Description"con
???
ASKER
The distination text should look like:
this all should have been on one line
content="(everything in here the program is taking out already)">
I'm not keeping the: content=" or the ending ">
I'm just having a problem with the extra spaces there sometimes are and the returns.
this all should have been on one line
content="(everything in here the program is taking out already)">
I'm not keeping the: content=" or the ending ">
I'm just having a problem with the extra spaces there sometimes are and the returns.
ok. if you want to strip out anything more than one space, try this on.... hopefully I understood correctly.
Sorry it took so long. Had to write the silly thing. Variation of a routine I wrote to strip out filenames from crap like "C:\windows\system\blah.dl l"
-------------------------- ---------- -----
Option Explicit
Private Sub Form_Load()
Dim S As String
S = "This is a test"
MsgBox StripSpaces(S)
End Sub
Private Function StripSpaces(ByRef S As String) As String
Dim T As String
Dim U As String
While (InStr(S, " ") > 0)
T = Left(S, InStr(S, " ") - 1)
U = Mid(S, InStr(S, " ") + 1, Len(S) - InStr(S, " "))
S = T & U
Wend
StripSpaces = S
End Function
-------------------------- -------
Sorry it took so long. Had to write the silly thing. Variation of a routine I wrote to strip out filenames from crap like "C:\windows\system\blah.dl
--------------------------
Option Explicit
Private Sub Form_Load()
Dim S As String
S = "This is a test"
MsgBox StripSpaces(S)
End Sub
Private Function StripSpaces(ByRef S As String) As String
Dim T As String
Dim U As String
While (InStr(S, " ") > 0)
T = Left(S, InStr(S, " ") - 1)
U = Mid(S, InStr(S, " ") + 1, Len(S) - InStr(S, " "))
S = T & U
Wend
StripSpaces = S
End Function
--------------------------
ASKER
ok.. tried it. Works perfectly. Between that code and the previous code for the return problems it's almost complete. With the previous code, is there a quick way to have it add a space before each sentence it puts at the end of the string? so... it should look like:
line1 line2 line3 line4
instead of:
lin1line2line3line4
line1 line2 line3 line4
instead of:
lin1line2line3line4
Yeah. Concatenate them together with a space in between.
Last Code Example:
--------
Dim Dest as As String, _
Sentence1 as String, _
Sentence2 as String
Sentence1 = "This is a test"
Sentence2 = "of the Emergency Broadcast System"
Dest = StripSpaces(Sentence1) & " " & StripSpaces(Sentence2)
MsgBox Dest
------
Just typed the code in here, didn't test it. should work though.
that help?
Last Code Example:
--------
Dim Dest as As String, _
Sentence1 as String, _
Sentence2 as String
Sentence1 = "This is a test"
Sentence2 = "of the Emergency Broadcast System"
Dest = StripSpaces(Sentence1) & " " & StripSpaces(Sentence2)
MsgBox Dest
------
Just typed the code in here, didn't test it. should work though.
that help?
ASKER
That parts works great, but remember the first code for making multiple lines one line. There's no space between the two lines it brings together. Thats the last thing I need :)
oh.
hold on........ you also want to replace the carriage returns with a space... I gotcha.
this should do it. in the "capture" routine change the lines to this. (from the comment on to the msgbox)
-------------
'this does it! Tried it me-self. :)
Dim X As String
Dim AA As Integer
For AA = 0 To intRows
X = X & Trim(StripSpaces(FormStr(A A, 0, CLng(intInputLength(AA)))) ) & " "
Next AA
MsgBox X
--------
hopefully that will do it.
hold on........ you also want to replace the carriage returns with a space... I gotcha.
this should do it. in the "capture" routine change the lines to this. (from the comment on to the msgbox)
-------------
'this does it! Tried it me-self. :)
Dim X As String
Dim AA As Integer
For AA = 0 To intRows
X = X & Trim(StripSpaces(FormStr(A
Next AA
MsgBox X
--------
hopefully that will do it.
never asked. what _are_ you doing, anyway?
ASKER
Sorry I took so long... I went out for a few minutes.. heh, like 20. I'm writing a program that will list all the html file information in another file... easier to find what your looking for if you have a ton of html files. I'm doing this because I have alot of html from previous sites I've had. That should be everything, so I just selected A: Excellent from above. Thanks alot for your help! It would have taken me a while to do this on my own, not to mention about 15 times more code.
anytime. was a pleasant diversion from my current task. what a life to be at work, coding on a Saturday night! still, could be worse, I could be married! Or worse, have a family!
Hehehe.
You are welcome
Hehehe.
You are welcome
ASKER