Solved

removing return codes from a file

Posted on 1998-08-01
22
176 Views
Last Modified: 2010-05-03
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?
0
Comment
Question by:dokken
  • 12
  • 10
22 Comments
 

Author Comment

by:dokken
ID: 1467436
Edited text of question
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467437
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?
0
 

Author Comment

by:dokken
ID: 1467438
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.
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467439
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.

0
 

Author Comment

by:dokken
ID: 1467440
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.
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467441
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.
0
 

Author Comment

by:dokken
ID: 1467442
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.
0
 
LVL 2

Accepted Solution

by:
percosolator earned 80 total points
ID: 1467443
Created a file called "Label.Txt" on C:\

Had our test:


0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467444
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


0
 

Author Comment

by:dokken
ID: 1467445
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.
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467446
could you give me an example of what you would like?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:dokken
ID: 1467447
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.
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467448
what would the destination text look like?

<meta name="Description" content = "thisallshouldhavebeenononeline">?

not:

<metaname="Description"content="thisallshouldhavebeenononeline">?

???
0
 

Author Comment

by:dokken
ID: 1467449
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.
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467450
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.dll"

-----------------------------------------

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
---------------------------------
0
 

Author Comment

by:dokken
ID: 1467451
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
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467452
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?
0
 

Author Comment

by:dokken
ID: 1467453
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 :)
0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467454
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(AA, 0, CLng(intInputLength(AA))))) & " "
   
    Next AA
   
    MsgBox X
--------

hopefully that will do it.

0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467455
never asked.  what _are_ you doing, anyway?
0
 

Author Comment

by:dokken
ID: 1467456
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.


0
 
LVL 2

Expert Comment

by:percosolator
ID: 1467457
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


0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now