Solved

Reading a flat file in VBScript

Posted on 1998-09-23
2
642 Views
Last Modified: 2008-03-10
How do you read a flat file in VBScript?
0
Comment
Question by:arichee
2 Comments
 
LVL 5

Accepted Solution

by:
chewhoung earned 100 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

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.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 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

20 Experts available now in Live!

Get 1:1 Help Now