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 - How to read and write a text file?

Reply
 
Old 06-14-2008   #1 (permalink)
Jirong Hu


 
 

How to read and write a text file?

Hi

Can someone give me an example to read and write a text file in this format
using VBScript? Thanks.

Jirong

"id","state","submitdate","severity","priority","summary","description"
"1","Submitted","4/11/00 7:00:00","3-Workaround","3","The shortcut
for""Printing"" is grayed out","See summary --John"
"2","Opened","4/14/00 11:32:00","1-Crash","1","Can't log in to the
system","I typed my login and the hourglass sign appears, but after 15
minutes, I still can't type my password. There's an infinite loop somewhere."

My System SpecsSystem Spec
Old 06-14-2008   #2 (permalink)
ekkehard.horner


 
 

Re: How to read and write a text file?

Jirong Hu schrieb:
Quote:

> Hi
>
> Can someone give me an example to read and write a text file in this format
> using VBScript? Thanks.
>
> Jirong
>
> "id","state","submitdate","severity","priority","summary","description"
> "1","Submitted","4/11/00 7:00:00","3-Workaround","3","The shortcut
> for""Printing"" is grayed out","See summary --John"
> "2","Opened","4/14/00 11:32:00","1-Crash","1","Can't log in to the
> system","I typed my login and the hourglass sign appears, but after 15
> minutes, I still can't type my password. There's an infinite loop somewhere."
A second possibility is to use the ADO text driver:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName( ".\adotext\jirong.txt" )
Dim sTDir : sTDir = oFS.GetParentFolderName( sFSpec )
Dim sTName : sTName = "[" & oFS.GetFileName( sFSpec ) & "]"

Dim oCN : Set oCN = CreateObject( "ADODB.Connection" )
Dim sCS : sCS = Join( Array( _
"Provider=MSDASQL" _
, "Driver={Microsoft Text Driver (*.txt; *.csv)}" _
, "DBQ=" & sTDir ), ";" )
Dim sSQL, oRS

oCN.Open sCS
WScript.Echo oCN.ConnectionString

sSQL = "SELECT COUNT( id ) FROM " & sTName
WScript.Echo sSQL
Set oRS = oCN.Execute( sSQL )
WScript.Echo "Records:", oRS.Fields( 0 ).Value
oRS.Close

sSQL = "SELECT summary FROM " & sTName & " WHERE id = '1'"
WScript.Echo sSQL
Set oRS = oCN.Execute( sSQL )
WScript.Echo ">" & oRS.Fields( "summary" ).Value & "<"
oRS.Close

sSQL = "INSERT INTO " & sTName & " ( id, state ) VALUES ( '3', 'pending' )"
WScript.Echo sSQL
oCN.Execute( sSQL )

sSQL = "SELECT * FROM " & sTName & " WHERE id = '3'"
Set oRS = oCN.Execute( sSQL )
WScript.Echo oRS.GetString( , , "|", vbCrLf )
oRS.Close

oCN.Close

output:

=== rwAdoText: read/write ADO text ============================================
Provider=MSDASQL.1;Extended Properties="DBQ=<...>;DefaultDir=<...>;
Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;
MaxBufferSize=2048;PageTimeout=5;"

SELECT COUNT( id ) FROM [jirong.txt]
Records: 2

SELECT summary FROM [jirong.txt] WHERE id = '1'
Quote:

>The shortcut for"Printing" is grayed out<
INSERT INTO [jirong.txt] ( id, state ) VALUES ( '3', 'pending' )
3|pending|||||
=== rwAdoText: 0 done (00:00:01) ==============================================

you'll need a schema.ini file in the folder the data file resides in.
The schema.ini file should contain a description/definition for your file
similar to this:

[jirong.txt]
Format=CSVDelimited
ColNameHeader=True
Col1 = id Text
Col2 = state Text
Col3 = submitdate Text
Col4 = severity Text
Col5 = priority Text
Col6 = summary Text
Col7 = description Text

(try to be more specific).

Use the keywords from the demo code to search msdn/google for tutorials/
documentation for ADO, Text Driver, ODBC, schema.ini and ask here if you
have more specific problems/questions.

Questions I#d like to ask:

(1) Do you plan to update records? (that's not so easy with ADO Text)

(2) How big is your file?
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Read from a txt, query AD, write to an xls file? VB Script
can't write to text file after installing app Vista General
Need help with log ( text ) file - write vs append VB Script
Use both read and write operation to the same file .NET General
How do I read a text file and sort text by fixed positions? PowerShell


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