Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


VBScript to modify csv file

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,64767
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript to modify csv file
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 VBScript to modify csv file - 10/3/2008 3:31:36 AM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
My objective is to be able to delete ranges of all the records (ex. "Name" and "Extension"), and delete any record where any of the fields contain a given string (ex. "N/A" and/or "??" and/or " "). 

I have found the following code to start, but now am lost.  Not even sure that this is the best starting point.

As always, my thanks in advance.

k



      
 
 
Post #: 1
 
 RE: VBScript to modify csv file - 10/7/2008 7:19:28 AM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
Partial Solution, by Mardmac, for anyone searching for similar solution

The following VBScript will delete FIELDS (example 10 and 3) within the CSV file C:\TEST.csv

'On Error Resume Next
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")


'Create an array out of the CSV
'open the data file
Set oTextStream = objFSO.OpenTextFile("C:\test.csv")
Set newFile = objFSO.CreateTextFile("C:\newCSV.csv")
'make an array from the data file
dataArray = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close

x = 0
For Each strLine In dataArray
  'Now make an array from each line
  ReDim Preserve clippedArray(x)
  clippedArray(x) =  Split(strLine,",")

' Delete Field 10 and 3 (highest to lowest)
  CutColumn = 10
  CutColumn = 3

  intCount = 0
  newLine = ""
  For Each Element In clippedArray(x)
      If intCount = UBound(clippedArray(x)) Then
          EndChar = vbCrLf
      Else
          EndChar = ","
      End If
 
      If intCount <> CutColumn -1 Then
         newLine = newLine & Element & EndChar
      End If
      intCount = intCount + 1
      If intCount = UBound(clippedArray(x))+1 Then
          newFile.Write newLine
      End If
  Next
 
Next

Now the simple question; Is it even possible to redefine field 2 as field 6, etc?

Thanks in advance

k

< Message edited by kriemer -- 10/7/2008 9:49:23 PM >

(in reply to kriemer)
 
 
Post #: 2
 
 RE: VBScript to modify csv file - 10/8/2008 8:48:54 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi kriemer,

I think your problem can be solved by a few SQL statements; could you post
a (short) sample of your input file?

Regards

ehvbs

(in reply to kriemer)
 
 
Post #: 3
 
 RE: VBScript to modify csv file - 10/8/2008 9:08:35 AM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
My total project will be to:
  1. Delete columns "Name", "Time", "Change", "Volume" (Header is shown for clarity only, 1st row is 1st record)
  2. Move column "Close" to last position
  3. Delete rows where any field contains either N/A, or "N/A"
I believe I have the VBS code for items 2 and 3, but any suggestions you have will be gratefully received.

CSV file structure as follows:

      

Many thanks in advance for any help you can provide.

k

< Message edited by kriemer -- 10/8/2008 10:13:48 AM >

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: VBScript to modify csv file - 10/8/2008 10:32:00 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi kriemer,

I think you are missing a column:


      

(in reply to kriemer)
 
 
Post #: 5
 
 RE: VBScript to modify csv file - 10/8/2008 10:38:54 AM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
Nope; I gave one field too many ("Last"), e.g, TKR,Name,Close,Date,Time,Change,Open,High,Low,Volume

Thanks

k

(in reply to ehvbs)
 
 
Post #: 6
 
 RE: VBScript to modify csv file - 10/8/2008 11:00:20 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Ok, I used this code


      

and this section in my schema.ini file:


      

to get:

=== kriemerCSV02: transform csv 02 ============================================
----------------------
"ALCO","ALICO INC",40.21,"10/8/2008","2:45pm",-0.51,39.00,40.23,38.01,21216
"ALD","ALLIED CAP CORP",6.65,"10/8/2008","2:46pm",+0.34,6.02,6.96,6.00,1727360
"ALDA","ALDILA INC",5.77,"10/8/2008","2:43pm",N/A,4.73,5.78,4.00,44351
"ALDN","ALADDIN KNOW SYS ",10.30,"10/8/2008","2:45pm",-0.75,"N/A",10.82,10.30,59

----------------------
-------------------------------------------------------------------------------
SELECT * FROM [kriemer01.csv]
-------------------------------------------------------------------------------
TKR: ALCO
Name: ALICO INC
Close: 40,21
Date: 10/8/2008
Time: 2:45pm
Change: -0,51
Open: 39
High: 40,23
Low: 38,01
Volume: 21216

TKR: ALD
Name: ALLIED CAP CORP
Close: 6,65
Date: 10/8/2008
Time: 2:46pm
Change: 0,34
Open: 6,02
High: 6,96
Low: 6
Volume: 1727360

TKR: ALDA
Name: ALDILA INC
Close: 5,77
Date: 10/8/2008
Time: 2:43pm
Change: <NULL>
Open: 4,73
High: 5,78
Low: 4
Volume: 44351

TKR: ALDN
Name: ALADDIN KNOW SYS
Close: 10,3
Date: 10/8/2008
Time: 2:45pm
Change: -0,75
Open: <NULL>
High: 10,82
Low: 10,3
Volume: 59502

-------------------------------------------------------------------------------
=== kriemerCSV02: 0 done (00:00:00) ===========================================

Does this look correct?

P.S. I forgot:


      

< Message edited by ehvbs -- 10/8/2008 11:02:36 AM >

(in reply to kriemer)
 
 
Post #: 7
 
 RE: VBScript to modify csv file - 10/8/2008 11:32:06 AM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi kriemer,

how about this:


      


      


      

(in reply to ehvbs)
 
 
Post #: 8
 
 RE: VBScript to modify csv file - 10/8/2008 11:54:47 AM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
Once again I feel like the South end of a horse facing North, but I have to ask, how do I use your script? 

Could you explain your code a bit?  For example:

Since I'm not too bright could you explain how the code deletes records based on N/A or "N/A"?

Am I correct in my understanding that your code appends the headers to kriemer02.csv (just trying to demonstrate I'm not a complete dope, though I think I failed)?

Many thanks

k

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: VBScript to modify csv file - 10/8/2008 12:15:46 PM   
  ehvbs

 

Posts: 2173
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi kriemer,

to use the script you either read it carefully - without executing - and and adapt it to your needs
or you set up a suitable environment and execute it (preferably in a debugger) to understand
it better. What you need is a directory (mine is .\adotext) containing a schema.ini file and the
sample .csv (kriemer01.csv) you posted. Put the definitions for kriemer01.txt and kriemer02.txt
in zhe schema.ini file (perhaps after finetuning the data types).

From this

"ALDN","ALADDIN KNOW SYS ",10.30,"10/8/2008","2:45pm",-0.75,"N/A",10.82,10.30, 59502

and

TKR: ALDN
Name: ALADDIN KNOW SYS
Close: 10,3
Date: 10/8/2008
Time: 2:45pm
Change: -0,75
Open: <NULL>
High: 10,82
Low: 10,3
Volume: 59502

we see, that the ADO driver understands/translates "N/A" as NULL (missing). So to exclude rows
with "N/A" the where clause of the SQL statement checks for (Not) NULL:

SELECT [TKR],[Date],[Open],[High],[Low],[Close] INTO [kriemer02.csv] FROM [kriemer01.csv] WHERE NOT ([TKR] IS NULL
OR [Name] IS NULL OR [Close] IS NULL OR [Date] IS NULL OR [Time] IS NULL OR [Change] IS NULL OR [Open] IS NULL
OR [High] IS NULL OR [Low] IS NULL OR [Volume] IS NULL )

There is no header in kriemer02.txt:

----------------------------------------------------------------------------------------------------
"ALCO","10/8/2008",39.00,40.23,38.01,40.21
"ALD","10/8/2008",6.02,6.96,6.00,6.65

because the schema.ini definitions specify

  [kriemer02.csv]
  ColNameHeader=False

(for both files).

Feel free to ask - there is no shame in not knowing something (yet).

Regards

ehvbs

(in reply to kriemer)
 
 
Post #: 10
 
 RE: VBScript to modify csv file - 10/8/2008 1:02:01 PM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
Thanks again for all the help and explanation.


Regards

k

(in reply to ehvbs)
 
 
Post #: 11
 
 RE: VBScript to modify csv file - 10/8/2008 1:40:52 PM   
  wjcott

 

Posts: 39
Score: 0
Joined: 9/7/2008
Status: offline
Ehvbs's solution is quite elegent and I took the liberty of putting it into a form that would allow the script to be called, passing the source and destination as arguments.

If you were to copy the following code into a script file and name it CVS.vbs, for example, you could call it from a batch file or command line in the following way ...

csv.vbs "tst1.csv" "tst6.csv"

...with tst1.csv being used as the source and tst6.csv being the new CSV file. In the above example, the source CSV file would be expected to be in the same directory as the script. However, a full path can be passed, allowing for the use of a source in another directory, like this...

csv.vbs "c:\temp\tst1.csv" "tst6.csv"

One thing to keep in mind is that the destination file must be in the same directory as the source (at least that is my understanding).


      

< Message edited by wjcott -- 10/8/2008 2:44:47 PM >

(in reply to kriemer)
 
 
Post #: 12
 
 RE: VBScript to modify csv file - 10/8/2008 2:19:12 PM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
Wow!

And now the inevitable part n of my question.  Can you recommend a book to gain a better understanding of VBS?  Googleling helps some, but I am thinking of "start here", then ... kind of book.

And again, thanks to you both for code.


k

(in reply to wjcott)
 
 
Post #: 13
 
 RE: VBScript to modify csv file - 10/8/2008 2:33:42 PM   
  wjcott

 

Posts: 39
Score: 0
Joined: 9/7/2008
Status: offline
What I know is almost entirely from the school of hard knocks, initially starting with VBA and MSOffice. I would mostly recommend hanging out in forums like this one, as you likely to see practical problems addressed that may be missed or considered too complicated for books. If you prefer to do some initial learning through a book, I have enjoyed the WROX press books (i.e. http://www.bookpool.com/sm/0470168080) that I have used for other subjects.There are some more subject specifc books, such as working with VBScript, ActiveDirectory, and WMI (i.e. http://www.bookpool.com/sm/0321501713), so your purposes may have an impact on he books to read.

PS. I would also recommend looking over the ASP references and tutorials at w3schools.com (http://www.w3schools.com/asp/default.asp & http://www.w3schools.com/ado/default.asp), as ASP web pages are often created using VBScript. Understanding the filesystem, textstream, file, folder, and ADO objects will get you a long way in creating solutions.

< Message edited by wjcott -- 10/8/2008 2:44:08 PM >

(in reply to kriemer)
 
 
Post #: 14
 
 RE: VBScript to modify csv file - 10/9/2008 8:34:33 AM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
I have neglected 4 requirements and would of course appreciate assistance.
  1. Remove quotation marks "  from around all fields
  2. Ticker symbols in col 1 MAY be hyphenated.  Replace hyphen with space
  3. Set number of decimal places to 2
  4. Add Text Value: DAMN,,,,,, to first Row
  5. Script variant is required to insert "Close" after "Low" but keep "Volume", e.g., TKR,Date,Open,High,Low,Close,Volume (Figured this one out)

Thanks again to any and all.


k

< Message edited by kriemer -- 10/9/2008 11:41:29 AM >

(in reply to wjcott)
 
 
Post #: 15
 
 RE: VBScript to modify csv file - 10/9/2008 12:00:58 PM   
  wjcott

 

Posts: 39
Score: 0
Joined: 9/7/2008
Status: offline
Unfortunately, besides the decimal precision, I could find no way of accomplishing the changes through the schema.ini and SQL. While it loses the elegance of the original solution, instead of having the ADO create the output file, I have added scripting to process the returned recrdset and create the output file. I am not entirely certain what the 4th item means, so this does not address that.


      

(in reply to kriemer)
 
 
Post #: 16
 
 RE: VBScript to modify csv file - 10/9/2008 12:33:57 PM   
  kriemer


Posts: 29
Score: 0
Joined: 9/26/2008
Status: offline
Your solution works, except numbers are still set to 6 significant decimal places

Item 4 is to append a phony record, which I call DAMN to the modified file <<<SOLVED

Thanks again (hoping to get smarter ASAP)

K

My solution to Item 4 is not elegant but it has the merit of working (and I found it myself!!!)

Inserted at end of wjcott's code:

Const FOR_READING = 1
Const FOR_WRITING = 2
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile("c:\b.csv",FOR_READING)
strContents = objTS.ReadAll
objTS.Close
Set objTS = objFS.OpenTextFile("c:\b.csv",FOR_WRITING)
objTS.WriteLine "DAMN,,,,,,"
objTS.Write strContents

Trying to understand how the coding that passes the source and destination file(s) works; actually looking to hard code these and having a surprising amount of difficulty doing so.

< Message edited by kriemer -- 10/10/2008 3:11:47 AM >

(in reply to wjcott)
 
 
Post #: 17
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> VBScript to modify csv file Page: [1]
Jump to:





<
New Messages No New Messages