Skip to main content
InSource Solutions

TN Appsvr116 Reading a CSV file into AppServer UDA's

insource_logo_large.jpg HOWTOSMALL.jpg
How to Read a CSV file into UDA's

Description

 

This is a simple scripting example to demonstrate how to read a CSV file into UDA arrays.  The scripting uses QuickScript .NET functions and is intended for Application Server.

 

Author

Rich Brooks

Publish Date 7/9/2014

Applies to Software

Application Server

Applies to Version

3.1 SP3 Patch1 and greater

Applies to System/Module

Scripting

Article Version

01.00.00

************************************************************************************************************************************************************

 

Before you Begin

Edit section

Create the required UDA array for each column in the CSV file, if not already existing. 

Detailed Steps

Edit section

There are many options in .NET for reading CSV files. The scripting example given here uses 
the System.IO.StreamReader class. It is built for a three column CSV file as shown below. ​

pic1.png

 

 

Each line of the CSV file is read in using the ReadLine method. The columns are parsed out 
using the various String methods. A while loop looks for the end of the file using the Peek 
method. Below is a screen capture from the IDE.

pic2.png

 

 

The following is the example scripting used.

 

dim s as string; 
dim sr as System.IO.StreamReader; 
dim stringlength as integer;
dim firststring as string;
dim secondstring as string;
dim thirdstring as string;
dim remainderstring as string;
dim index as integer;
 
sr = System.IO.File.OpenText("c:\csvtest2.txt"); 
 
while sr.Peek() > -1
index = index + 1; 
s = sr.ReadLine();
LogMessage(s);
stringlength = StringInString(s, ",", 1, 0);
firststring = StringMid(s,1,stringlength-1);
me.FirstUDA[index] = firststring;
remainderstring = StringRight(s, StringLen(s) - stringlength);
stringlength = StringInString(remainderstring, ",", 1, 0);
secondstring = StringMid(remainderstring,1,stringlength-1);
me.SecondUDA[index] = secondstring;
thirdstring = StringRight(remainderstring, StringLen(remainderstring) -
stringlength);
me.ThirdUDA[index] = thirdstring;
endwhile; 
 
me.ParseIT = 0;

 

 

The object hosting the script was deployed from the IDE and tested out from the Object Viewer. 
Each column has been parsed out to their respective UDA.​

pic3.png