Reading CSV files...

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
chihuahualand
Junior Coder
Posts: 35
Joined: Thu Jan 12, 2006 9:20 pm
Contact:

Reading CSV files...

Post by chihuahualand » Thu Mar 12, 2009 1:23 am

I've seen topics suggesting "Separate"... this only works on the simplest of data.

I've seen this
http://www.mjtnet.com/forum/viewtopic.php?t=1467
(which is oddly incomplete)

I started building the code in MS but without reasonable psuedocode. I keep think I'm worrying about embedded quotes and commas too much.

It sure would be nice to have a ReadCSVfile command within MS

in lieu of that, does anybody have some reasonable code?

I'm going to restart the project using vbscript,
Last edited by chihuahualand on Thu Mar 12, 2009 2:15 am, edited 1 time in total.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Re: Reading CSV files...

Post by jpuziano » Thu Mar 12, 2009 2:09 am

chihuahualand wrote:I've seen this
http://www.mjtnet.com/forum/viewtopic.php?t=1467
(which is oddly incomplete)
I see what you mean, the VBScript looks unfinished, there is a VBSTART but no VBEND.

Marcus, do you have the complete script for that example and if so, could you re-post it? Please and thanks.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Mar 12, 2009 3:19 am

I posted a sample using that VBScript HERE

chihuahualand
Junior Coder
Posts: 35
Joined: Thu Jan 12, 2006 9:20 pm
Contact:

Post by chihuahualand » Thu Mar 12, 2009 7:03 am

A revised version
This version doesn't load the entire file into an array, rather, it returns the next record with the fields separated by linefeeds. The first record returned is either field names or the first line of data (depending on how your file is constructed). The Microsoft text driver assumes a header line, so there's a wee bit of programming to compensate.

Code: Select all

VBSTART
'An example file.  Save in My Documents
'Remember to remove comments at start of line if pasting into editor for example file
'header1,header2,header3,header4
'r1c1,r1c2,r1c3
'r2,c1",r2c2,r2c3
'r3c1,"r""3,c""2",r3c3
'r4c1,r4c2,"r,4"",""c3"

rcount = 0
tcount = 0
Set oShell = CreateObject( "WScript.Shell" )
uDocs=oShell.ExpandEnvironmentStrings("%UserProfile%")
Set oShell = nothing
if trim(uDocs)="" then
	udocs = "C:\My Documents\"
else
	uDocs = uDocs & "\My Documents\"
end if

Set rs = createobject("ador.recordset")
strConnect =  "Driver={Microsoft Text Driver (*.txt; *.csv)};" &  "DefaultDir=" & uDocs & ";"

Function csv(filename)
csv = ""
rcount = rcount + 1
if rcount=1 then
	rs.open "select * from " & filename, strConnect, 3
	for each f in rs.fields
		csv = csv & f.name & chr(10)
	next
elseif rcount=2 then
	for each f in rs.fields
		csv = csv & f.value & chr(10)
	next
else
	rs.movenext
	for each f in rs.fields
		csv = csv & f.value & chr(10)
	next
end if
csv = left(csv, len(csv)-1)
end function

Function TotalRecords(filename)
	Set tmp = createobject("ador.recordset")
	tmp.open "select * from " & filename, strConnect, 3
	TotalRecords = tmp.recordcount+1
	Set tmp = nothing
End Function

VBEND

VBeval>TotalRecords("csv.csv"),x
MessageModal>x

VBeval>csv("csv.csv"),x
Separate>x,LF,s
MessageModal>s_1

VBeval>csv("csv.csv"),x
Separate>x,LF,s
MessageModal>s_1

VBeval>csv("csv.csv"),x
Separate>x,LF,s
MessageModal>s_1

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Mar 12, 2009 1:33 pm

Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Re: Reading CSV files...

Post by jpuziano » Thu Mar 12, 2009 4:47 pm

jpuziano wrote:
chihuahualand wrote:I've seen this
http://www.mjtnet.com/forum/viewtopic.php?t=1467
(which is oddly incomplete)
I see what you mean, the VBScript looks unfinished, there is a VBSTART but no VBEND.

Marcus, do you have the complete script for that example and if so, could you re-post it? Please and thanks.
Thanks for fixing the code in the above mentioned post in Scripts and Tips...

Import data from CSV file into multidimensional array

...and adding the link there to the new blog post showing all the great ways we have to do this.

And thanks to chihuahualand for letting us know there was a problem with the original code posted there... because now its fixed.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts