Page 1 of 1

Reading unlimited columns of CSV file with Scripting Edition

Posted: Mon Nov 06, 2006 1:37 pm
by Hannes, Tech Support
Loop through the lines of a CSV file, and put all entries of one line into an array:

Code: Select all

Option Explicit

'global variables: use for customization
Dim INPUT_PATH
INPUT_PATH = "C:\Programme\iMacros\datasources\myfile.csv" 'CSV format expected
 
'initialize macro and related variables
dim iret,iim1,index,initMacro
set iim1 = CreateObject ("imacros")
iret = iim1.iimInit()

' setting up file access
Dim objFileSystem, objInputFile, inputFile, inputFileStream
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set inputFile = objFileSystem.GetFile(INPUT_PATH)
Const ForReading = 1 ' means: open for reading
Const TristateDefault = -2 ' means: read as ASCII
Set inputFileStream = inputFile.OpenAsTextStream(ForReading, TristateDefault)


'main loop through all lines in inputFile
Dim aktInputLine, entries

do until (inputFileStream.atEndOfStream)

   'read next line from input file
   aktInputLine = inputFileStream.ReadLine
 
   'put CSV-data into entries array.
   entries = Split(aktInputLine, ",")
   
   ' now, entries(0), entries(1), etc contain the actual line's data
   ' and may be used in the script, or - with iimSet() - in a macro.
   
loop 
More examples of reading files and using their content in macros via the scripting interface can be found in iMacros' examples folder: "file-2-web.vbs" and "file--2-web-method2.vbs".