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?