Solved

Reading a flat file in VBScript

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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