Solved

Reading a flat file in VBScript

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…

718 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