473,387 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Read data from csv file using VBScript

I would like to read a column of data in csv file on each rows and get the data to perform calculation as shown below:

"Sample Number","POD1","POD5","Time"
803,E7,03,
1803,E7,03,3.3330 us
2803,E7,03,3.3330 us
3803,E7,03,3.3330 us
4803,E7,03,3.3345 us
5803,E7,03,3.3330 us
6803,E7,03,3.3330 us
7803,E7,03,3.3330 us
8803,E7,03,3.3330 us
9803,E7,03,3.3345 us

I will need to read the values in the last column 3.3330 u and use this value to convert it to frequency. Note the 3.3330us is in second which means it reads as 3.3330micro second. I would also like to repeat it for every rows until end, how should i perform that. Please help me to come out with simple vbscript command on that.Thank you
Jul 10 '08 #1
8 22620
gpraghuram
1,275 Expert 1GB
If its in C or C++ i can help u.
Since its in VB why cant u try posting this in .NET forum

Raghu
Jul 10 '08 #2
Banfa
9,065 Expert Mod 8TB
Since its in VB why cant u try posting this in .NET forum

Raghu
Better still try posting in the VB forum :D

I am moving this post to a more relevant forum anyway.
Jul 10 '08 #3
If its in C or C++ i can help u.
Since its in VB why cant u try posting this in .NET forum

Raghu
Raghu, thanks for your proposal.

Can you show to me if it is running using C++? I need to learn as well.

meiliong
Jul 11 '08 #4
gpraghuram
1,275 Expert 1GB
Raghu, thanks for your proposal.

Can you show to me if it is running using C++? I need to learn as well.

meiliong
I think this is moved to new forum...why cant u post a new thread for this...in C++ forum

raghu
Jul 11 '08 #5
!NoItAll
297 100+
"Sample Number","POD1","POD5","Time"
803,E7,03,
1803,E7,03,3.3330 us
2803,E7,03,3.3330 us
3803,E7,03,3.3330 us
4803,E7,03,3.3345 us
5803,E7,03,3.3330 us
6803,E7,03,3.3330 us
7803,E7,03,3.3330 us
8803,E7,03,3.3330 us
9803,E7,03,3.3345 us


Assuming you know how to put the data into a variable - we'll call it sCSVData

Expand|Select|Wrap|Line Numbers
  1. Dim sCSVLine() as String
  2. Dim sCSVElements() as String
  3. Dim I as String
  4.  
  5. sCSVLine = Split(sCSVData, vbcrlf)
  6.  
  7. For I = 0 to Ubound(sCSVLine)
  8.  
  9. sCSVElements = Split(sCSVLine(I), ",")
  10.  
  11. 'at this point you will have each element of one line of the csv to process as you need.
  12.  
  13.  
  14. Next I

Danger: CSV files can be delimited with just commas, or commas and quotes. This routine does NOT work with commas and quotes - just the data you provided.
Jul 12 '08 #6
"Sample Number","POD1","POD5","Time"
803,E7,03,
1803,E7,03,3.3330 us
2803,E7,03,3.3330 us
3803,E7,03,3.3330 us
4803,E7,03,3.3345 us
5803,E7,03,3.3330 us
6803,E7,03,3.3330 us
7803,E7,03,3.3330 us
8803,E7,03,3.3330 us
9803,E7,03,3.3345 us


Assuming you know how to put the data into a variable - we'll call it sCSVData

Expand|Select|Wrap|Line Numbers
  1. Dim sCSVLine() as String
  2. Dim sCSVElements() as String
  3. Dim I as String
  4.  
  5. sCSVLine = Split(sCSVData, vbcrlf)
  6.  
  7. For I = 0 to Ubound(sCSVLine)
  8.  
  9. sCSVElements = Split(sCSVLine(I), ",")
  10.  
  11. 'at this point you will have each element of one line of the csv to process as you need.
  12.  
  13.  
  14. Next I

Danger: CSV files can be delimited with just commas, or commas and quotes. This routine does NOT work with commas and quotes - just the data you provided.
Thank you, but how about if i would like to grab the data and perform calculation for example 3.3330+3.3330+3.3330+3.3330+3.3330+3.3330+....=xxx x?How should i proceed from here?
Jul 13 '08 #7
!NoItAll
297 100+
Thank you, but how about if i would like to grab the data and perform calculation for example 3.3330+3.3330+3.3330+3.3330+3.3330+3.3330+....=xxx x?How should i proceed from here?
Expand|Select|Wrap|Line Numbers
  1.   Dim sCSVLine() as String
  2.   Dim sCSVElements() as String
  3.   Dim I as String
  4.   Dim lTime() as Long
  5.   Dim lCSVCount as Long
  6.   Dim sTime as String
  7.  
  8. sCSVLine = Split(sCSVData, vbcrlf)
  9. lCSVCount = Ubound(sCSVLine)
  10. Redim lTime(lCSVCount)
  11.  
  12.    'start at 1 because the first set of values is just the header
  13.   For I = 1 to lCSVCount
  14.          sCSVElements = Split(sCSVLine(I), ",")
  15.          'i believe element 3 contains the value you want
  16.          sTime = Trim$(Replace(sCSVElements(3), "us"))
  17.          if IsNumeric(sTime) = True then
  18.               lTime(I) = cLng(sTime)  
  19.          else
  20.               lTime(I) = -1
  21.          end if
  22.          DoEvents   'this is here to give the OS  a break from this rather tight loop
  23.    Next I
  24.  
'when the loop is complete you will have (lCSVCount -1) (skip element 0) of the microsecond (us) values in the one dimensional lTime array. Now you can do something simple like ltime(1) + ltime(2) + ltime(3) etc...
Any invalid times will be -1 so you will want to skip those so it's best to do your math in a loop.
Jul 13 '08 #8
Expand|Select|Wrap|Line Numbers
  1.   Dim sCSVLine() as String
  2.   Dim sCSVElements() as String
  3.   Dim I as String
  4.   Dim lTime() as Long
  5.   Dim lCSVCount as Long
  6.   Dim sTime as String
  7.  
  8. sCSVLine = Split(sCSVData, vbcrlf)
  9. lCSVCount = Ubound(sCSVLine)
  10. Redim lTime(lCSVCount)
  11.  
  12.    'start at 1 because the first set of values is just the header
  13.   For I = 1 to lCSVCount
  14.          sCSVElements = Split(sCSVLine(I), ",")
  15.          'i believe element 3 contains the value you want
  16.          sTime = Trim$(Replace(sCSVElements(3), "us"))
  17.          if IsNumeric(sTime) = True then
  18.               lTime(I) = cLng(sTime)  
  19.          else
  20.               lTime(I) = -1
  21.          end if
  22.          DoEvents   'this is here to give the OS  a break from this rather tight loop
  23.    Next I
  24.  
'when the loop is complete you will have (lCSVCount -1) (skip element 0) of the microsecond (us) values in the one dimensional lTime array. Now you can do something simple like ltime(1) + ltime(2) + ltime(3) etc...
Any invalid times will be -1 so you will want to skip those so it's best to do your math in a loop.
Thanks, i think it did actually gave me some idea. previously i did come out with my own code, is that mean if i wanted to add all the values on element 3 i just perform as above or how should i continue from my own code rather use the one you recommended?Thank you for your comment,i really appreciate it!

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("c:\test2\test.txt", ForReading)

Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
For i = 3 to Ubound(arrServiceList)
Wscript.Echo "Service: " & arrServiceList(i)
Next
Loop
Jul 13 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Scott | last post by:
Is there a way to check if a file exist using VBScript in an ASP page. So the code might go something like this: If Exists("C:\Junk\1.txt") then Do something Else Do something else End If ...
5
by: Mika M | last post by:
Hi! I'm trying to read text file like... "Field1";"Field2";"Field3";"Field4" "ABCD";"EFGH";"1234";"20051020" "AABB";"CCDD";"2468";"20051021" "CCDD";"XXYY";"4321";"20051022" ....using OLE...
8
by: Johnny | last post by:
Hi all: I have an ASP.NET form that reads an Excel file and populates a datagrid. If I load the form in IE on the server, and select a local file, the code works fine. However if I load the form...
1
by: neveen | last post by:
i want to open and read text file using j2me that can run on mobile 6630 then i want to make button called read that when pressed the data inside text display
0
by: uspramod | last post by:
hi the source is an excel file it is pivoted, i want to do unpivoting using vbscript regards Pramod
1
by: =?Utf-8?B?c3Vpcw==?= | last post by:
Hi every body, i have a problem with how to read .MSG file (Outlook) using vb.net ? using this articles the author has already explain how to read .MSG file using stuctured storage (vc++) ...
5
by: Rohit111111 | last post by:
Hello All, I new to ASP and i am using VBScript, I want to delete a file which is located on the remote machine hbow can i delete that file.plz help its urgent
7
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using visual studio C# window and I have an xml file which I need to read that file using Object Oriented program. What codes I should use in my class to load and read that xml file....
5
Soniad
by: Soniad | last post by:
Hello, I want to include asp file within vbscript, this file name i am taking in variable . and when i use this variable in the include tag outside delimiters(<%%>), it does not work, <%Dim...
4
by: tokcy | last post by:
Hi everyone, I have some .rtf extension file and i want to read that file using php on html page but i am not able to do this. If it is .txt file then there is no problem but it is RTF and DOC...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.