• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

multiple space-delimited files

I need to import text files where the records are delimited
by unknown amounts of spaces...sometimes 1, sometimes 2, etc

for example:

record1 record2      record3    record4

and I need to read each into separate arrays.
Kind of like importing a file with excel using the
"space delimited" option.

Does anyone know how to do this in Visual Basic?

Thanks

Martin
0
mlachain
Asked:
mlachain
1 Solution
 
KDivadCommented:
Tested and tried. This will sort the file into the array and will use a varying number of spaces as the delimiter. I'm not sure this is what you want, but maybe?...

    Dim FileArray() As String
    Open "C:\windows\desktop\e.txt" For Input As 1
    FT = Input(LOF(1), 1)
    Close
    Do
        If Left$(FT, 1) = " " Then
            FT = Right$(FT, Len(FT) - 1)
        Else
            y = InStr(1, FT, " ")
            X = X + 1
            ReDim Preserve FileArray(1 To X)
            If y = 0 Then
                FileArray(X) = FT
                Exit Do
            Else
                FileArray(X) = Left$(FT, y - 1)
                FT = Right$(FT, Len(FT) - y)
            End If
        End If
    Loop
0
 
smeggheadCommented:
If I understand your question correctly, you want to import a text file where each field is of a variable width and padded with spaces ??? if not I appologise for waffling..

This is very difficult to do without any user intervention. Excel only guesses at where the field breaks are. It's very difficult to automate this procedure. i.e. how could it know that the field value 'ABC123' is actually three separate fields of 'ABC' '12' AND '3' ??

It is possible that the fields always have at least one space between them, in which case you can go through each record looking at each character in the first couple of hundred records to determine which characters are always equal to space (ascii-32) and are followed by a non-space character for the majority of the time... but again, the best you can do is guess...
0
 
MoondancerCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp

In efforts to help all Members maintain their open questions, this is a reminder to click your Member Profile, expand your question history and find/update all your Open and Locked questions.
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now