We help IT Professionals succeed at work.

Help reading a .csv file with WSH

1,053 Views
Last Modified: 2009-12-16
Currently I am reading a list of id's in a text file, but I now need it to read a list of ids from a csv file.  The csv file will look something like this:

Location,ID,Ldapinfo
Location,ID,Ldapinfo
Location,ID,Ldapinfo
Location,ID,Ldapinfo

I will need to read the entire file and just get the ID info.  My currert code when the list just contained the ID looks like this:


Set objFSO = CreateObject("Scripting.FileSystemObject")
'open the user file and set the names into the arrLdapIds array.
Set objTextFile = objFSO.OpenTextFile("c:\temp\practicevb\vb2005\pilotids.txt", 1)
strText = (objTextFile.ReadAll)
objTextFile.Close
arrLdapIds = Split(strText, vbCrLf)




 For Each strUser in arrLdapIds

next
Comment
Watch Question

Commented:
   s = fso.GetFile("C:\csv.csv").OpenAsTextStream(ForReading).ReadAll
   
    Dim sa() As String
   
    sa = Split(s, ",")
   
    For n = 1 To UBound(sa) - 1 Step 2
   
        MsgBox sa(n)
   
    Next n

Author

Commented:
This does not seem to work.  any ideas?  Chad

Commented:
It works for me - I saved your data thus ["The csv file will look something like this"]:

Location,1,Ldapinfo
Location,2,Ldapinfo
Location,3,Ldapinfo
Location,4,Ldapinfo

Which bit - what - doesn't work?

Author

Commented:
This is the error I get.  

C:\Temp>testcsc.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Temp\testcsc.vbs(3, 14) Microsoft VBScript compilation error: Expected end of statement

It does not like Dim sa() As String
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
That works, but I just have more small issue.  My csv file really has 5 items per row and not 3.  How do I modify it for that?  It looks more like this:

Location,1,Ldapinfo,test1,newtest1
Location,2,Ldapinfo,test2,newtest2
Location,3,Ldapinfo,test3,newtest3
Location,4,Ldapinfo,test4,newtest4

Thanks again, Chad

Commented:
For n = 1 To UBound(sa) - 1 Step 4

Author

Commented:
Works great!  Thanks, Chad
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.