I have a script that queries the printers and has an output as follows:
*********************start of output**********************
Name,PortName,ShareName,Location
\\server01\printer01A,172.29.43.13:RAW,printer01A,123 drive,
\\server01\printer01B,IP_172.29.102.21,printer01B,123 drive,
\\server02\printer01C,IP_172.17.122.22,printer01C,123 drive,
*********************end of output**********************
script that creates output
*********************start of script**********************
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut
Const ForReading = 1
Const ForAppending = 8
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Set objTextFileRead = objFSO.OpenTextFile ("c:\scripts\printers.txt",
ForReading)
Set objTextFileWrite = objFSO.CreateTextFile
("c:\scripts\printer_status.txt", ForWriting, True)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
objTextFileWrite.Write "Name" & "," & "PortName" & "," & "ShareName" & "," &
"Location"
objTextFileWrite.WriteLine
Set colInstalledPrinters = objWMIService.ExecQuery ("Select * from
Win32_Printer")
For Each objPrinter in colInstalledPrinters
objTextFileWrite.Write objPrinter.Name & ","
objTextFileWrite.Write objPrinter.PortName & ","
objTextFileWrite.Write objPrinter.ShareName & ","
objTextFileWrite.Write objPrinter.Location & ","
objTextFileWrite.WriteLine(1)
Next
objTextFileWrite.Close
*********************end of script**********************
But when i try to read the file with the following script I get gibberish.
I believe the script above is formatting the text file in way that is
unreadable for whatever reason. suggestions?
*********************start of script**********************
On Error Resume Next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
strPathtoTextFile = "C:\scripts\"
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""
objRecordset.Open "SELECT * FROM Printer_Status.txt", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordset.EOF
Wscript.Echo "Name: " & objRecordset.Fields.Item("Name")
Wscript.Echo "PortName: " & objRecordset.Fields.Item("PortName")
Wscript.Echo "ShareName: " & objRecordset.Fields.Item("ShareName")
Wscript.Echo "Location: " & objRecordset.Fields.Item("Location")
objRecordset.MoveNext
Loop
*********************end of script**********************
ekkehard.horner - 28 Aug 2008 23:17 GMT
knothead schrieb:
> I have a script that queries the printers and has an output as follows:
> *********************start of output**********************
[quoted text clipped - 5 lines]
> script that creates output
> *********************start of script**********************
[...]
> Set objTextFileWrite = objFSO.CreateTextFile
> ("c:\scripts\printer_status.txt", ForWriting, True)
The prototype of .*Create*TextFile is:
object.CreateTextFile(filename[, overwrite[, unicode]])
So you open the file in Unicode mode. Use
Set objTextFileWrite = objFSO.CreateTextFile
("c:\scripts\printer_status.txt", True)
instead.
[...]
> *********************end of script**********************
> But when i try to read the file with the following script I get gibberish.
[quoted text clipped - 3 lines]
> *********************start of script**********************
> On Error Resume Next
Global OERN - A bad idea
[...]
> objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & strPathtoTextFile & ";" & _
> "Extended Properties=""text;HDR=YES;FMT=Delimited"""
Without a schema.ini file with a "Characterset=Unicode", the ANSI
default will be used.
[...]
Old Pedant - 29 Aug 2008 03:46 GMT
> I have a script that queries the printers and has an output as follows:
> *********************start of output**********************
[quoted text clipped - 3 lines]
> \\server02\printer01C,IP_172.17.122.22,printer01C,123 drive,
> *********************end of output**********************
Your output is bogus, to begin with. You have an extra comma on the end of
each line. You need to kill that terminating comma.
Now...
Personally, I wouldn't bother using the MS text driver to read this kind of
file. Way overkill. (And I sure wouldn't try to lock the file for output!
almost surely wouldn't work anyway. And I'll bet even opening with a static
cursor is a mistake.)
I'd just open it as a text file and read the lines one at a time and split
each line on the commas.
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile fso.OpenTextFile strPathtoTextFile & "\Printer_Status.txt"
Do
line = inFile.readLine
items = Split( line, "," )
Wscript.Echo "Name: " & items(0)
Wscript.Echo "PortName: " & items(1)
Wscript.Echo "ShareName: " & items(2)
Wscript.Echo "Location: " & items(3)
Until inFile.AtEndOfStream
************
And now your bogus commas on the end of each line won't even matter.
ekkehard.horner - 29 Aug 2008 08:41 GMT
Old Pedant schrieb:
[...]
> Personally, I wouldn't bother using the MS text driver to read this kind of
> file. Way overkill.
[...]
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set inFile fso.OpenTextFile strPathtoTextFile & "\Printer_Status.txt"
[quoted text clipped - 7 lines]
> Wscript.Echo "Location: " & items(3)
> Until inFile.AtEndOfStream
[...]
I agree to your other remarks, but I feel the pros of using ADO should
be mentioned/demonstrated too:
' from ADO (just for reference)
'---- CursorTypeEnum Values ----
Const adOpenForwardOnly = 0 ' 00000000
Const adOpenKeyset = 1 ' 00000001
Const adOpenDynamic = 2 ' 00000002
Const adOpenStatic = 3 ' 00000003
'---- LockTypeEnum Values ----
Const adLockReadOnly = 1 ' 00000001
Const adLockPessimistic = 2 ' 00000002
Const adLockOptimistic = 3 ' 00000003
Const adLockBatchOptimistic = 4 ' 00000004
' from ADO (needed for GetString)
Const adClipString = 2
' need these objects for Filesystem and ADO Database work
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim oCNCT : Set oCNCT = CreateObject( "ADODB.Connection" )
' specify text data dir (don't use trailing \)
Dim sTDir : sTDir = oFS.GetAbsolutePathName( ".\adotext" )
' you can use two drivers (ODBC/OLEDB); take your pick by commenting in/out or reordering
Dim sCS
sCS = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sTDir & ";Extended
Properties=""text;HDR=YES;FMT=Delimited"""
sCS = "Provider=MSDASQL;Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" & sTDir & ";"
' some (fairly) complicated things easily done by SQL
Dim aSQLs : aSQLs = Array( _
"SELECT * FROM [printers.txt]" _
, "SELECT ShareName, Name, Location, PortName FROM [printers.txt] ORDER BY Portname" _
, "SELECT ShareName, PortName FROM [printers.txt] WHERE PortName LIKE '%.29.%'" _
, "SELECT 'Sum of Locations:', SUM( CLNG( LEFT( Location, 3 ) ) ) FROM
[printers.txt]" _
)
oCNCT.Open sCS
Dim sSQL
For Each sSQL In aSQLs
WScript.Echo "----", sSQL
' Cursortype and Locktype automagically set to resonable defaults
WScript.Echo oCNCT.Execute( sSQL ).GetString( adClipString, , vbTab, vbCrLf, "NULL" )
Next
WScript.Echo "-----"
Dim oRS : Set oRS = oCNCT.Execute( aSQLs( 1 ) )
WScript.Echo "CT:", oRS.Cursortype, " LT:",oRS.Locktype
WScript.Echo "-----"
Dim oFld
For Each oFld In oRS.Fields
WScript.Echo Right( Space( 12 ) & oFld.Name, 12 ) & ": " & oFld.Value
Next
oRS.Close
WScript.Echo "-----"
oCNCT.Close
output:
=== readPrinters: read printers.txt using ado =================================
---- SELECT * FROM [printers.txt]
\\server01\printer01A 172.29.43.13:RAW printer01A 123 drive NULL
\\server01\printer01B IP_172.29.102.21 printer01B 123 drive NULL
\\server02\printer01C IP_172.17.122.22 printer01C 123 drive NULL
---- SELECT ShareName, Name, Location, PortName FROM [printers.txt] ORDER BY Portname
printer01A \\server01\printer01A 123 drive 172.29.43.13:RAW
printer01C \\server02\printer01C 123 drive IP_172.17.122.22
printer01B \\server01\printer01B 123 drive IP_172.29.102.21
---- SELECT ShareName, PortName FROM [printers.txt] WHERE PortName LIKE '%.29.%'
printer01A 172.29.43.13:RAW
printer01B IP_172.29.102.21
---- SELECT 'Sum of Locations:', SUM( CLNG( LEFT( Location, 3 ) ) ) FROM [printers.txt]
Sum of Locations: 369
-----
CT: 0 LT: 1
-----
ShareName: printer01A
Name: \\server01\printer01A
Location: 123 drive
PortName: 172.29.43.13:RAW
-----
=== readPrinters: 0 done (00:00:01) ===========================================
Old Pedant - 29 Aug 2008 19:36 GMT
Not going to quote all Ekkehard posted.
Yes, I agree. If you *need* to emulate a DB via a text file, that is
absolutely the right thing to do.
I was just responding to only the code that had been posted.