Reading a user selected CSV file

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

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

Reading a user selected CSV file

Post by JRL » Mon Jul 10, 2006 3:20 pm

Hello,
I found most of this in scripts and tips, I added the first 11 lines. The added lines allow a user to enter a path and file name then sets the path to the variable "directory" and sets the file name to the variable "filename". I'm confused about how to input the directory and filename variables into the vbscript. There is a line in the vbscript that defines the directory: "DefaultDir=C:\;" I tried changing this to "DefaultDir=%directory%;". There is also a line in the vbscript that defines the file name: "select * from test.csv". I tried changing this to: "select * from %filename%". When I made these alterations the script gave errors indicating it was missing the file information. Is there any way to pass these two variables to this vbscript?

I have a method of breaking up a .csv file into usable components that I wrote about 4 years ago but its very difficult to adapt to each new usage. I found this script and found it much easier to use. But for my current situation I don't want to hardcode the path and filename. Perhaps someone has a reliable easy to replicate method of breaking up any CSV file into its various cells that they would be willing to share? Or perhaps there is one already on the forum and I failed to find it.

Thanks for any help,
Dick

Reposted with HTML disabled. Although, I would note that this code does not work though it is now (hopefully) not corrupt.

Code: Select all

Input>file1,CSV File to process

Separate>file1,\,dir
Let>filename=dir_%dir_count%
sub>dir_count,1
Let>directory=
Let>k=0
Repeat>k
  add>k,1
  let>value=dir_%k%
  Concat>directory,%value%\
Until>k=dir_count

VBSTART

'An example file.  Save as c: est.csv or change file name and DefaultDir in function
'Remeber 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"

'Global Array for holding all data
Dim FieldArray

' ImportCSV - imports data from a CSV file and populates a mulitdimensional array
Sub ImportCSV

   Dim fieldnum
   Dim recnum

   set rs = createobject("ador.recordset")
   strConnect =  "Driver={Microsoft Text Driver (*.txt; *.csv)};" &  "DefaultDir=C:\;"
   Const adOpenStatic = 3
   rs.open "select * from test.csv", strConnect, adOpenStatic
   ReDim FieldArray(rs.recordcount+1,rs.fields.count)

   'set record and field counters.  I'm started at field 1 rather than 0.
   recnum = 0
   fieldnum = 1

   'add headers in row zero (optional, but handily means actual data starts in row 1)
   'if you omit this section data will start in row 0 unless you change recnum declaration above
   for each f in rs.fields
      if f.name <> "" then
        FieldArray(recnum,fieldnum) = f.name
      end if
      fieldnum = fieldnum + 1
   next
   recnum = recnum + 1
   'end header section

   'now add the data starting in row 1
   do until rs.eof
     fieldnum = 1
     for each f in rs.fields
       if f.value <> "" then
          FieldArray(recnum,fieldnum) = f.value
       end if
       fieldnum = fieldnum + 1
     next
     recnum = recnum + 1
     rs.movenext
   loop

End Sub

VBEND

'Get Data into Array
VBRun>ImportCSV

'First header name
VBEval>FieldArray(0,1),field
MessageModal>field

'Get the first field of the first record:
VBEval>FieldArray(1,1),field
MessageModal>field

'Get the second field of the third record:
VBEval>FieldArray(3,2),field
MessageModal>field
Last edited by JRL on Mon Jul 10, 2006 4:03 pm, edited 1 time in total.

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

Post by Marcus Tettmar » Mon Jul 10, 2006 3:52 pm

I'm confused about how to input the directory and filename variables into the vbscript.
Replace the literals with VBScript variables which are passed into the subroutine via subroutine parameters. Then modify the VBRun line to pass them into the subroutine. So the code (based on the one in Scripts & Tips) looks like this:

Code: Select all

VBSTART

'An example file.  Save as c: est.csv or change file name and DefaultDir in function
'Remeber 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"

'Global Array for holding all data
Dim FieldArray

' ImportCSV - imports data from a CSV file and populates a mulitdimensional array
Sub ImportCSV(directory, filename)

   Dim fieldnum
   Dim recnum

   set rs = createobject("ador.recordset")
   strConnect =  "Driver={Microsoft Text Driver (*.txt; *.csv)};" &  "DefaultDir=" & directory & ";"
   Const adOpenStatic = 3
   rs.open "select * from " & filename, strConnect, adOpenStatic
   ReDim FieldArray(rs.recordcount+1,rs.fields.count)

   'set record and field counters.  I'm started at field 1 rather than 0.
   recnum = 0
   fieldnum = 1

   'add headers in row zero (optional, but handily means actual data starts in row 1)
   'if you omit this section data will start in row 0 unless you change recnum declaration above
   for each f in rs.fields
      if f.name <> "" then
        FieldArray(recnum,fieldnum) = f.name
      end if
      fieldnum = fieldnum + 1
   next
   recnum = recnum + 1
   'end header section

   'now add the data starting in row 1
   do until rs.eof
     fieldnum = 1
     for each f in rs.fields
       if f.value <> "" then
          FieldArray(recnum,fieldnum) = f.value
       end if
       fieldnum = fieldnum + 1
     next
     recnum = recnum + 1
     rs.movenext
   loop

End Sub

VBEND

'Get Data into Array
VBRun>ImportCSV,c:\files\,test.csv

'First header name
VBEval>FieldArray(0,1),field
MessageModal>field

'Get the first field of the first record:
VBEval>FieldArray(1,1),field
MessageModal>field

'Get the second field of the third record:
VBEval>FieldArray(3,2),field
MessageModal>field
Note the parms on the Sub ImportCSV declaration and in the code and the call in the VBRun line.

Incidentally your script is corrupted - try reposting it with HTML disabled.
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
JRL
Automation Wizard
Posts: 3529
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Mon Jul 10, 2006 4:04 pm

Thanks for the help Marcus. I'll post the result when I get it working.

Later,
Dick

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

Post by JRL » Mon Jul 10, 2006 7:14 pm

Here is a sample script using an altered version of Marcus' original posted in Scripts and Tips.

I found that if the maximum row and column values defined by the VBscript are exceeded, VBscript pops an error window. So I was forced to learn something today and created two global variables in order to pass the maximum row and column values of the matrix to the macro script. There might be an easier way but I'm so proud of myself for making it work... I don't care if there's a better way.

Thank you Marcus for the original script and for altering it today.

(HTML disabled when posted)
Later,
Dick

Code: Select all

Dialog>Dialog1
   Caption=Select a CSV file to process
   Width=445
   Height=145
   Top=152
   Left=28
   Label=Enter a CSV file path and name to process or Select Browse to pick from a list,40,16
   Edit=edit1,24,40,393,
   Button=Browse,176,80,75,25,0
   Default=Browse
   FileBrowse=Browse,edit1,CSV Files|*.csv|All Files|*.*,open
EndDialog>Dialog1

Show>Dialog1,r1
If>r1=2,finish
If>Dialog1.edit1=,finish

Separate>%Dialog1.edit1%,\,dir
Let>fname=dir_%dir_count%
sub>dir_count,1
Let>directry=
Let>k=0
Repeat>k
  add>k,1
  let>value=dir_%k%
  Concat>directry,%value%\
Until>k=dir_count

VBSTART
'Sample formats:
'header1,header2,header3,header4
'r1c1,r1c2,r1c3
'r2,c1",r2c2,r2c3
'r3c1,"r""3,c""2",r3c3
'r4c1,r4c2,"r,4"",""c3"

'Global variables for passing data to macro script
Dim FieldArray
Dim globfieldnum
Dim globrecnum

' ImportCSV - imports data from a CSV file and populates a mulitdimensional array
Sub ImportCSV(directory, filename)

   Dim fieldnum
   Dim recnum

   set rs = createobject("ador.recordset")
   strConnect =  "Driver={Microsoft Text Driver (*.txt; *.csv)};" &  "DefaultDir=" & directory & ";"
   Const adOpenStatic = 3
   rs.open "select * from " & filename, strConnect, adOpenStatic
   ReDim FieldArray(rs.recordcount+1,rs.fields.count)

   'set record and field counters.
   recnum = 1
   fieldnum = 1

   for each f in rs.fields
      if f.name <> "" then
        FieldArray(recnum,fieldnum) = f.name
      end if
      fieldnum = fieldnum + 1
   next
   recnum = recnum + 1
   'end header section

   'now add the data starting in row 1
   do until rs.eof
     fieldnum = 1
     for each f in rs.fields
       if f.value <> "" then
          FieldArray(recnum,fieldnum) = f.value
       end if
       fieldnum = fieldnum + 1
     next
     recnum = recnum + 1
     rs.movenext
	 globrecnum = recnum
	 globfieldnum = fieldnum
   loop
End Sub
VBEND

'Get Data into Array
VBRun>ImportCSV,%directry%,%fname%

VBEval>globrecnum,row_count
VBEval>globfieldnum,col_count

Sub>col_count,1
Let>MSG_WIDTH=500

Let>row=0
Repeat>row
  Let>col=0
  add>row,1
  Repeat>col
    Add>col,1
    VBEval>FieldArray(%row%,%col%),field
	GoSub>Process
  Until>col=col_count
  Let>next=%row%+1
  Ask>Continue and view row %next%?,rv
  If>rv=NO,finish
Until>row=row_count

Label>finish

SRT>Process
  //Do stuff using variables %row% %col% and %field%
  MDL>Row %row% of %row_count%, Column %col% of %col_count% = %field%
END>Process

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