Results 1 to 9 of 9

Thread: space delimited text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Post


    I am working on a subroutine to convert space-delimited files to semicolon-delimited files. Right now the file looks something like this:

    Adam Smith 45 Cherry St. 789-0098
    Susan Sarandon 123 123rd Avenue 555-5555
    Walter Matthau 54 Hollywood Blvd. 123-4567

    Right now I am using the code

    Open CommonDialog1.Filename For Input as #File1
    Line Input #File1, strText

    Open CommonDialog2.Filename For Output as #File2
    Print #File2, Texto

    Obviously all this does is copy the file but unfortunately that is about the extent of my VB experience. I need a subroutine that will read the file, replace the white spaces between columns with a ";" and also return the column length (e.g. the number of spaces between the A of Adam Smith and the first number of his phone number).

    any suggestions will be GREATLY appreciated!

    Thanks,

    Andrew


  2. #2
    Guest

    Post

    where i work, all we do is generate comma/tab delimited files or fixed width files. i would be very interested to find out why you use space delimited files, and why you would want to use semi-colon as a delimiter. Maybe i can give you some insight into my specialty subject.

    please mail me if you have the time

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

    Wossname,
    Email me: wossnamex@talk21.com

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,175

    Post

    Try this:
    Code:
        Dim sText As String
        Dim nPos As Integer
    
        sText = "Susan Sarandon 123 123rd Avenue 555-5555"
        nPos = InStrRev(sText, " ")
        sText = Left(sText, nPos - 1) & ";" & Right(sText, Len(sText) - nPos)
        MsgBox sText
        MsgBox "Name and address is " & nPos - 1 & " characters long"
    This will only work if the telephone number has no spaces, so it can't be something like "(408) 555-1212". If it can, that you could first use instr to find the first occurrence of "(" and if found, use a modified version of the above.

    ------------------
    Marty

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Post

    If you have VB6 then you can use the Replace function:

    strText = Replace(strText, " ", ";")

    If you dont have VB6 then use the following function:

    Public Function ReplaceText(strText$, strFind$, strReplaceWith$)
    Dim strRetVal$, iPos%
    strRetVal = strText
    Do
    iPos = InStr(strRetVal, strFind)
    If iPos > 0 Then
    strRetVal = Left$(strRetVal, iPos -1) & strReplaceWith & Mid$(strRetVal, iPos + Len(strFind))
    End If
    Loop While iPos > 0
    ReplaceText = strRetVal
    End Function

    Good luck!

    ------------------
    Joacim Andersson
    joacim@programmer.net
    joacim@yellowblazer.com
    www.YellowBlazer.com



  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Post

    The problem when I use the Replace function is that it replaces all spaces with a ";". This will not work when the entry in one of the columns is less than the column length, e.g. if the column length is 12 and the text entered is length 7, the Replace function will add 5 semicolons instead of one.

    How can I return the location of a character? I think that I could use this to get around the above problem.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Post

    The problem when I use the Replace function is that it replaces all spaces with a ";". This will not work when the entry in one of the columns is less than the column length, e.g. if the column length is 12 and the text entered is length 7, the Replace function will add 5 semicolons instead of one.

    How can I return the location of a character? I think that I could use this to get around the above problem.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Columbia, SC USA
    Posts
    374

    Post

    why the hell is everything that I post going on the boards twice?

  8. #8
    Addicted Member
    Join Date
    Jul 1999
    Posts
    219

    Post

    maybe you hit send twice????!!!!!!......:-)

  9. #9
    Hyperactive Member
    Join Date
    Jul 1999
    Location
    NY, USA
    Posts
    270

    Post

    Again, if you are using VB6, then you can use the Split Function:

    Code:
    Dim FileNameOPEN, FileNameSAVE As String
    Private Sub Command1_Click()
        On Error GoTo CDCancelError
        With CommonDialog1
            .Filter = "Space Delimited Text Files (*.txt)|*.txt"
            .Flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
            .CancelError = True
            .DialogTitle = "OPEN Space Delimited File"
            .ShowOpen
            FileNameOPEN = .FileName
            .Filter = "Semi-Colon Delimited Text Files (*.txt)|*.txt"
            .Flags = cdlOFNPathMustExist Or cdlOFNHideReadOnly
            .CancelError = True
            .DialogTitle = "SAVE Semi-Colon Delimited File"
            .ShowSave
            FileNameSAVE = .FileName
        End With
        Open FileNameOPEN For Input As #1
        Open FileNameSAVE For Output As #2
        Do While Not EOF(1)
            Dim Temp As String
            Line Input #1, Temp
            Dim Lines() As String
            Lines = Split(Temp, " ", -1, vbTextCompare)
            Print #2, Lines(0) & ";" & Lines(1) & ";" & Lines(2) & ";" & Lines(3) & ";" & Lines(4) & ";" & Lines(5)
        Loop
        Close
    CDCancelError:
    End Sub
    ------------------
    Tom Young, 14 Year Old
    tyoung@stny.rr.com
    ICQ: 15743470 Add Me ICQ Me
    AIM: TomY10
    PERL, JavaScript and VB Programmer

    [This message has been edited by Compwiz (edited 11-27-1999).]

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