Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - VBscribt to read CSV file and creat other file

Reply
 
Old 07-22-2009   #1 (permalink)
M.K


 
 

VBscribt to read CSV file and creat other file

Hi All

I want VBScribt that read spesifc coulm in CSV file and write iy in new CSV
file



My System SpecsSystem Spec
Old 07-22-2009   #2 (permalink)
David Kerber


 
 

Re: VBscribt to read CSV file and creat other file

In article <DE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx>,
MK@xxxxxx says...
Quote:

> Hi All
>
> I want VBScribt that read spesifc coulm in CSV file and write iy in new CSV
> file
Go ahead and write one; it's not difficult. You will need to read each
line from the original file, parse it out to find the column you want,
and then write it out again.

--
/~\ The ASCII
\ / Ribbon Campaign
X Against HTML
/ \ Email!

Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).

My System SpecsSystem Spec
Old 07-22-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: VBscribt to read CSV file and creat other file


"M.K" <MK@xxxxxx> wrote in message
newsE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx
Quote:

> Hi All
>
> I want VBScribt that read spesifc coulm in CSV file and write iy in new
> CSV
> file
>
I have an example VBScript program that demonstrates a function to read a
csv file linked here:

http://www.rlmueller.net/ReadCSV.htm

The function parses each line and puts the values into an array. For
example, to output the values in the third field of the csv file, the
program can be similar to below:
============
Option Explicit
Dim objFSO, strInput, objInput
Dim arrValues, strItem, strLine
Dim strOutput, objOutput

Const ForReading = 1
Const ForWriting = 2

' Specify the csv file.
strInput = "c:\Scripts\Test.csv"

' Specify the new file.
strOutput = "c:\Scripts\Output.csv"

' Open the input file for read access
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInput, ForReading)

' Open the output file for writing.
Set objOutput = objFSO.OpenTextFile(strOutput, ForWriting)

' Read the file.
Do Until objInput.AtEndOfStream
strLine = objInput.ReadLine
' Skip blank lines.
If (Trim(strLine) <> "") Then
' Parse the fields in the file.
arrValues = CSVParse(strLine)
' Write the value in the third field to the output file.
objOutput.WriteLine arrValues(2)
End If
Loop

' Clean up.
objInput.Close
objOutput.Close
========
Note that arrays a zero based. That is, the first field in arrValues is
arrValues(0). The third field is arrValues(2). You must include the Function
CSVParse from the link I gave earlier.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 07-22-2009   #4 (permalink)
James Whitlow


 
 

Re: VBscribt to read CSV file and creat other file

"M.K" <MK@xxxxxx> wrote in message
newsE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx
Quote:

> Hi All
>
> I want VBScribt that read spesifc coulm in CSV file and write iy in new
> CSV
> file
Help me understand, please. If you have a CSV like:

Heading1,Heading2,Heading3,Heading4
Value1-1,Value1-2,Value1-3,Value1-4
Value2-1,Value2-2,Value2-3,Value2-4
Value3-1,Value3-2,Value3-3,Value3-4

Are you wanting to specify a column, let's say 'Heading2' and have it
output like below to another file?

Heading2
Value1-2
Value2-2
Value3-2


My System SpecsSystem Spec
Old 07-22-2009   #5 (permalink)
M.K


 
 

Re: VBscribt to read CSV file and creat other file

Hi James Whitlow,


Thank you for replay

Yes that what i want, I want the following reult in new csv file

Heading2
Value1-2
Value2-2
Value3-2





"James Whitlow" wrote:
Quote:

> "M.K" <MK@xxxxxx> wrote in message
> newsE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx
Quote:

> > Hi All
> >
> > I want VBScribt that read spesifc coulm in CSV file and write iy in new
> > CSV
> > file
>
> Help me understand, please. If you have a CSV like:
>
> Heading1,Heading2,Heading3,Heading4
> Value1-1,Value1-2,Value1-3,Value1-4
> Value2-1,Value2-2,Value2-3,Value2-4
> Value3-1,Value3-2,Value3-3,Value3-4
>
> Are you wanting to specify a column, let's say 'Heading2' and have it
> output like below to another file?
>
> Heading2
> Value1-2
> Value2-2
> Value3-2
>
>
>
My System SpecsSystem Spec
Old 07-22-2009   #6 (permalink)
Richard Mueller [MVP]


 
 

Re: VBscribt to read CSV file and creat other file


"M.K" <MK@xxxxxx> wrote in message
news:81535E40-4DD4-4B00-B7AD-9C416BBFCE10@xxxxxx
Quote:

> Hi James Whitlow,
>
>
> Thank you for replay
>
> Yes that what i want, I want the following reult in new csv file
>
> Heading2
> Value1-2
> Value2-2
> Value3-2
>
>
>
>
>
> "James Whitlow" wrote:
>
Quote:

>> "M.K" <MK@xxxxxx> wrote in message
>> newsE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx
Quote:

>> > Hi All
>> >
>> > I want VBScribt that read spesifc coulm in CSV file and write iy in new
>> > CSV
>> > file
>>
>> Help me understand, please. If you have a CSV like:
>>
>> Heading1,Heading2,Heading3,Heading4
>> Value1-1,Value1-2,Value1-3,Value1-4
>> Value2-1,Value2-2,Value2-3,Value2-4
>> Value3-1,Value3-2,Value3-3,Value3-4
>>
>> Are you wanting to specify a column, let's say 'Heading2' and have it
>> output like below to another file?
>>
>> Heading2
>> Value1-2
>> Value2-2
>> Value3-2
>>
>>
>>
The example I posted earlier, using my function CSVParse, does this.
However, if the values should be enclosed in quotes, you can replace this
statement:

objOutput.WriteLine arrValues(2)

with this:

objOutput.WriteLine """" & arrValues(2) & """"

Note that this writes a string to the file that is the concatenation of
three strings. The string """" resolves to a single quote character. Strings
are enclosed in quotes and any embedded quote characters in the string must
be doubled. The values must be enclosed in quotes if the have embedded
commas. The function CSVParse handles the csv file properly whether the
values are quoted or not (as long as values with commas are quoted).

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 07-22-2009   #7 (permalink)
James Whitlow


 
 

Re: VBscribt to read CSV file and creat other file

> "James Whitlow" wrote:
Quote:

>
Quote:

>> "M.K" <MK@xxxxxx> wrote in message
>> newsE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx
Quote:

>> > Hi All
>> >
>> > I want VBScribt that read spesifc coulm in CSV file and write iy in new
>> > CSV
>> > file
>>
>> Help me understand, please. If you have a CSV like:
>>
>> Heading1,Heading2,Heading3,Heading4
>> Value1-1,Value1-2,Value1-3,Value1-4
>> Value2-1,Value2-2,Value2-3,Value2-4
>> Value3-1,Value3-2,Value3-3,Value3-4
>>
>> Are you wanting to specify a column, let's say 'Heading2' and have it
>> output like below to another file?
>>
>> Heading2
>> Value1-2
>> Value2-2
>> Value3-2
>>
>>
>>"M.K" <MK@xxxxxx> wrote in message
>>news:81535E40-4DD4-4B00-B7AD-9C416BBFCE10@xxxxxx
> Hi James Whitlow,
>
>
> Thank you for replay
>
> Yes that what i want, I want the following reult in new csv file
>
> Heading2
> Value1-2
> Value2-2
> Value3-2
Ok, below is my suggested code.

Dim oFSO, oDB, oRS, sOldFile, sNewFile, sColumn, sSource

sOldFile = "C:\My File.csv"
sNewFile = "C:\New File.txt"
sColumn="Heading2"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDB = CreateObject("ADODB.Connection")

sSource = oFSO.GetParentFolderName(sOldFile)
sOldFile = oFSO.GetFileName(sOldFile)

oDB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sSource _
& ";Extended Properties=""text;HDR=YES;FMT=Delimited"""

Set oRS = oDB.Execute("SELECT * FROM [" & sOldFile & "]")

With oFSO.OpenTextFile(sNewFile, 2, True)
.WriteLine sColumn
Do Until oRS.EOF
.WriteLine oRS.Fields(sColumn)
oRS.MoveNext
Loop
End With


My System SpecsSystem Spec
Old 07-22-2009   #8 (permalink)
Richard Mueller [MVP]


 
 

Re: VBscribt to read CSV file and creat other file


"James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
news:eNVMpHwCKHA.528@xxxxxx
Quote:
Quote:

>> "James Whitlow" wrote:
>>
Quote:

>>> "M.K" <MK@xxxxxx> wrote in message
>>> newsE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx
>>> > Hi All
>>> >
>>> > I want VBScribt that read spesifc coulm in CSV file and write iy in
>>> > new
>>> > CSV
>>> > file
>>>
>>> Help me understand, please. If you have a CSV like:
>>>
>>> Heading1,Heading2,Heading3,Heading4
>>> Value1-1,Value1-2,Value1-3,Value1-4
>>> Value2-1,Value2-2,Value2-3,Value2-4
>>> Value3-1,Value3-2,Value3-3,Value3-4
>>>
>>> Are you wanting to specify a column, let's say 'Heading2' and have it
>>> output like below to another file?
>>>
>>> Heading2
>>> Value1-2
>>> Value2-2
>>> Value3-2
>>>
>>>
>>>"M.K" <MK@xxxxxx> wrote in message
>>>news:81535E40-4DD4-4B00-B7AD-9C416BBFCE10@xxxxxx
>> Hi James Whitlow,
>>
>>
>> Thank you for replay
>>
>> Yes that what i want, I want the following reult in new csv file
>>
>> Heading2
>> Value1-2
>> Value2-2
>> Value3-2
>
> Ok, below is my suggested code.
>
> Dim oFSO, oDB, oRS, sOldFile, sNewFile, sColumn, sSource
>
> sOldFile = "C:\My File.csv"
> sNewFile = "C:\New File.txt"
> sColumn="Heading2"
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oDB = CreateObject("ADODB.Connection")
>
> sSource = oFSO.GetParentFolderName(sOldFile)
> sOldFile = oFSO.GetFileName(sOldFile)
>
> oDB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sSource _
> & ";Extended Properties=""text;HDR=YES;FMT=Delimited"""
>
> Set oRS = oDB.Execute("SELECT * FROM [" & sOldFile & "]")
>
> With oFSO.OpenTextFile(sNewFile, 2, True)
> .WriteLine sColumn
> Do Until oRS.EOF
> .WriteLine oRS.Fields(sColumn)
> oRS.MoveNext
> Loop
> End With
>
An excellent solution. The Jet driver should be on almost any client and
handles comma delimited files well. More work is required to do the same
thing in VBScript.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 07-22-2009   #9 (permalink)
James Whitlow


 
 

Re: VBscribt to read CSV file and creat other file

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
message news:u$XA2fxCKHA.4168@xxxxxx
Quote:

>
> "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
> news:eNVMpHwCKHA.528@xxxxxx
Quote:
Quote:

>>> "James Whitlow" wrote:
>>>
>>>> "M.K" <MK@xxxxxx> wrote in message
>>>> newsE04C144-0D5F-445E-BFF2-19244A589688@xxxxxx
>>>> > Hi All
>>>> >
>>>> > I want VBScribt that read spesifc coulm in CSV file and write iy in
>>>> > new
>>>> > CSV
>>>> > file
>>>>
>>>> Help me understand, please. If you have a CSV like:
>>>>
>>>> Heading1,Heading2,Heading3,Heading4
>>>> Value1-1,Value1-2,Value1-3,Value1-4
>>>> Value2-1,Value2-2,Value2-3,Value2-4
>>>> Value3-1,Value3-2,Value3-3,Value3-4
>>>>
>>>> Are you wanting to specify a column, let's say 'Heading2' and have it
>>>> output like below to another file?
>>>>
>>>> Heading2
>>>> Value1-2
>>>> Value2-2
>>>> Value3-2
>>>>
>>>>
>>>>"M.K" <MK@xxxxxx> wrote in message
>>>>news:81535E40-4DD4-4B00-B7AD-9C416BBFCE10@xxxxxx
>>> Hi James Whitlow,
>>>
>>>
>>> Thank you for replay
>>>
>>> Yes that what i want, I want the following reult in new csv file
>>>
>>> Heading2
>>> Value1-2
>>> Value2-2
>>> Value3-2
>>
>> Ok, below is my suggested code.
>>
>> Dim oFSO, oDB, oRS, sOldFile, sNewFile, sColumn, sSource
>>
>> sOldFile = "C:\My File.csv"
>> sNewFile = "C:\New File.txt"
>> sColumn="Heading2"
>>
>> Set oFSO = CreateObject("Scripting.FileSystemObject")
>> Set oDB = CreateObject("ADODB.Connection")
>>
>> sSource = oFSO.GetParentFolderName(sOldFile)
>> sOldFile = oFSO.GetFileName(sOldFile)
>>
>> oDB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sSource _
>> & ";Extended Properties=""text;HDR=YES;FMT=Delimited"""
>>
>> Set oRS = oDB.Execute("SELECT * FROM [" & sOldFile & "]")
>>
>> With oFSO.OpenTextFile(sNewFile, 2, True)
>> .WriteLine sColumn
>> Do Until oRS.EOF
>> .WriteLine oRS.Fields(sColumn)
>> oRS.MoveNext
>> Loop
>> End With
>>
>
> An excellent solution. The Jet driver should be on almost any client and
> handles comma delimited files well. More work is required to do the same
> thing in VBScript.
Thanks, Richard! I actually learned to use the Jet Driver for CSV files
sometime back from an old Usenet by you!


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can not Creat work file,check temp environment variable Microsoft Office
Read a line from a text file, without loading the entire file inmemory PowerShell
How do I read a VHD file Vista General
can not read a file Vista General
coping file from a remote file share - FILE IS NO LONG THERE bogus error message Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46