The fun starts working with CSV files. CSV files are comma separated value, files. Basically that means there are commas between the columns. So who creates them? Almost every application that creates logs, dumps performance data, configuration, etc. Before XML files became the craze CSV ruled the administrator’s world.
Below is a sample CSV file
abcd,12,E_123,300 |
Let us write a code that can display any column we like. If we pass the parameter 0 (zero) we get the entire file. If we pass a specific number we get that column. If we pass a number more than the columns we show the entire file.
First the code
'Show the selected coloumn Const ReadMode=1 |
And now the output
C:\scripts\file-io>02-show-contents.vbs sample.csv 0 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 1 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 2 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 3 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 4 //nologo C:\scripts\file-io>02-show-contents.vbs sample.csv 5 //nologo C:\scripts\file-io> |
Some exercises for you to try.
1. Suppose we need the sum of a column (like column 2 or 4) then modify the above code so that it works in the following way.
c:\>progname csvfile col
The output should a tab between the cols and not a csv and a sum below.
No comments:
Post a Comment