[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1010
  • Last Modified:

Help reading a .csv file with WSH

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
0
chadmanvb
Asked:
chadmanvb
  • 4
  • 4
1 Solution
 
peetmCommented:
   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
0
 
chadmanvbAuthor Commented:
This does not seem to work.  any ideas?  Chad
0
 
peetmCommented:
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
chadmanvbAuthor 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
0
 
peetmCommented:
Try this ...

dim s
dim n

dim sa
dim txs
   
dim o

    set o = Wscript.CreateObject("Scripting.FileSystemObject")
   
    set txs = o.OpenTextFile("C:\csv.csv")

    s = txs.ReadAll
   
    sa = Split(s, ",")
   
    For n = 1 To UBound(sa) - 1 Step 2
   
        MsgBox sa(n)
   
    Next
0
 
chadmanvbAuthor 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
0
 
peetmCommented:
For n = 1 To UBound(sa) - 1 Step 4

0
 
chadmanvbAuthor Commented:
Works great!  Thanks, Chad
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now