?
Solved

Reading a flat file in VBScript

Posted on 1998-09-23
2
Medium Priority
?
656 Views
Last Modified: 2008-03-10
How do you read a flat file in VBScript?
0
Comment
Question by:arichee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
chewhoung earned 200 total points
ID: 1436310
This is forimporting comma delimited text.  Basicly its the same for fixed
lenth text files. Just  minus the Token function and read the records into the
MyRec variable directly.

type myrec
    Name as string * 30
    Address as string * 30
    Phone as string * 14
end type

then dim a variable of the myrec type
dim MRec as myrec
dim LineBuff as string
set up a main prodecure that brings in the text from the text file one record
at a time

sub Import()
open "myfile.txt" for input as #1
dim LineBuffer as string : Rec% = 1
DO

Get #1, MRec%, LineBuffer

Mrec.name = GetToken(LineBuffer)
Mrec.address = GetToken(LineBuffer)
Mrec.phone = GetToken(Linebuffer)
Rec% = Rec% +1
... code to place in table
Loop While not EOF(1)

close #1
end sub

Function Token$(byref LineBuffer as string)
    If Ltrim$(Rtrim$(LineBuffer)) = "" then
        Token$ = "" :LineBuff = "" : exit function
    end if
    if left(LineBuff,2) = """" then
    LineBuff = right(LineBuff,len(LineBuff)-2,Len(LineBuff))
    Token$ = "": exit function
    Mark% = Instr$(",",LineBuff,Len(LineBuff))
    if not left(LineBuff,1) = "," then
          if Mark% then
          Token$ = Mid$(LineBuff,2,Mark% -2)
          LineBuff = Right$(LineBuff,Len(LineBuff) - Mark%)
          exit function
       else
       Token$  = Mid$(LineBuff, 2, Len(LineBuff)-1)
       LineBuff = "": exit function
       endif
    else  REM  the old style
        if Mark% then
             Token$ =   Mid$(LineBuff,2,Mark% -1)
              LineBuff = Right$(LineBuff,len(LineBuff) -Mark% -1)    
              exit function
        else
             Token$ = Mid$(LineBuff,2,Len(LineBuff)-1)
             LineBuff = "" : Exit function
        endif
     endif
end function  

0
 
LVL 1

Expert Comment

by:mr_krishna
ID: 1436311
Alternate you can use filescripting component which is provided in Interdev.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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