Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple VBScript to Read in 2 Column CSV File

2,738 views
Skip to first unread message

Robert Elliott

unread,
Dec 15, 2003, 12:29:50 PM12/15/03
to
Can anyone help me with some code to read in a CSV file? It really doesn't
matter what type of file it is. I just need to read in a file with two
columns and populate two arrays. Does anyone have code that can do
something like this?

Someone else mentioned just using a comma delimited text file and pulling
the data in that way. If that is easier then I am not opposed to doing it
that way.

Thanks,
Robbie


McKirahan

unread,
Dec 15, 2003, 12:55:24 PM12/15/03
to
"Robert Elliott" <rell...@procard.com> wrote in message
news:OoURMEzw...@tk2msftngp13.phx.gbl...


Will this help; watch for word-wrap:

Option Explicit

'---------------------------------------------------------------------------
'
' "otfSplit.vbs.vbs"
'
' This VBS (Visual Basic Script) program does the following:
' 1) Reads a file of two fields into two arrays.
'
' To test, either double-click on the filename in Windows Explorer or
' from the Command prompt, type "cscript otfSplit.vbs".
'
' Changes:


-------------------------------------------------------------------------
' 15-Dec-2003.0 Created.
'

'---------------------------------------------------------------------------
'
Const cVBS = "otfSplit.vbs"
Const cOTF = "otfSplit.txt"
'*
'* Start Message
'*
MsgBox Now & ": '" & cVBS & "' started."
'*
'* Declare Variables
'*
Dim arrONE(99)
Dim arrTWO(99)
Dim strARR
Dim intOTF
intOTF = 0
Dim strOTF
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOTF
Set objOTF = objFSO.OpenTextFile(cOTF,1)
'*
'* Read File
'*
Do While Not objOTF.AtEndOfStream
strOTF = objOTF.ReadLine()
'*
'* Check for one (and only one) separator
'*
If InStr(strOTF,",") > 0 _
And InStr(strOTF,",") <> InStrRev(strOTF,",")Then
WScript.Echo "Invalid input: " & strOTF
WScript.Quit
End If
'*
'* Build Arrays
'*
strARR = Split(strOTF,",")
arrONE(intOTF) = strARR(0)
arrTWO(intOTF) = strARR(1)
'WScript.Echo arrONE(intOTF) & " : " & arrTWO(intOTF)
intOTF = intOTF + 1
Loop
'*
'* Destroy Objects
'*
Set objOTF = Nothing
Set objFSO = Nothing
'*
'* Finish Message
'*
MsgBox Now & ": '" & cVBS & "' finished."

Robert Elliott

unread,
Dec 15, 2003, 1:11:53 PM12/15/03
to
Awesome!

This works fine. I really appreciate it.
Nice coding, by the way. I am pretty new at scripting and I completely
understand everything you are doing.

Thanks so much,
Robbie

"McKirahan" <Ne...@McKirahan.com> wrote in message
news:gAmDb.549936$HS4.4175408@attbi_s01...

McKirahan

unread,
Dec 15, 2003, 1:50:25 PM12/15/03
to
"Robert Elliott" <rell...@procard.com> wrote in message
news:eUdvtbzw...@TK2MSFTNGP09.phx.gbl...> Awesome!


You're welcome.


On reflection, I revised some of the code:

'*
'* Check for one (and only one) separator
'*

If InStr(strOTF,",") = 0 _
Or InStr(strOTF,",") <> InStrRev(strOTF,",") Then

Michael Dunn

unread,
Dec 15, 2003, 4:56:37 PM12/15/03
to

"Robert Elliott" <rell...@procard.com> wrote in message news:OoURMEzw...@tk2msftngp13.phx.gbl...
: Can anyone help me with some code to read in a CSV file? It really doesn't


This is almost the same as McKirahan's, but Ill post it anyway

Option Explicit
Const ForReading = 1, ForWriting = 2, ForAppending = 8
dim lines, column(), fileData, fso, x, y, lineParts
set fso = CreateObject("Scripting.FileSystemObject")
fileData = fso.OpenTextFile("c:\test.csv",ForReading).readall
lines = split(filedata,vbnewline)
redim column(1,ubound(lines))
for x = 0 to ubound(lines)
lineParts = split(lines(x),",")
column(0,x) = lineParts(0)
column(1,x) = lineParts(1)
next
set fso = nothing
msgbox column(1,2)


Roland Hall

unread,
Dec 15, 2003, 6:23:02 PM12/15/03
to
"McKirahan" wrote:

> "Robert Elliott" wrote:
> Awesome!
> >
> > This works fine. I really appreciate it.
> > Nice coding, by the way. I am pretty new at scripting and I completely
> > understand everything you are doing.
> >
> > Thanks so much,
> > Robbie

How often do you hear that? Nominates Robbie as VBScripting Newbie of the
Year! *roar from the crowd*


McKirahan

unread,
Dec 15, 2003, 8:33:22 PM12/15/03
to
"Roland Hall" <nobody@nowhere> wrote in message
news:#8mhj21w...@TK2MSFTNGP12.phx.gbl...


Excellent point, Roland.

I've been thanked alot for my help but Robert when above and beyond with his
praise.

Thanks Robert!


Roland Hall

unread,
Dec 16, 2003, 5:31:48 PM12/16/03
to

"McKirahan" wrote:

> "Roland Hall" wrote:
> > "McKirahan" wrote:
> > > "Robert Elliott" wrote:
> > > Awesome!
> > > >
> > > > This works fine. I really appreciate it.
> > > > Nice coding, by the way. I am pretty new at scripting and I
> completely
> > > > understand everything you are doing.
> > > >
> > > > Thanks so much,
> > > > Robbie
> >
> > How often do you hear that? Nominates Robbie as VBScripting Newbie of
the
> > Year! *roar from the crowd*
>
>
> Excellent point, Roland.
>
> I've been thanked alot for my help but Robert when above and beyond with
his
> praise.
>
> Thanks Robert!

No doubt. The rest of us are jealous! (O;=


0 new messages