Solved

Reading a flat file in VBScript

Posted on 1998-09-23
2
647 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

828 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