VBS script append to csv file - wierd behavior

Hi 

Im new at this vbs scripting...but trying slowly to learn and using the script56.chm.

But this script is acting wierd? im trying to collect the windows caption and version export it to a CSV file.

If the file isnt created then create it. If it is created then append to it.

When i run it first time it goes okay? but when i run it again - then i get some chinese text???

from the csv file from the second run of the script 

"Microsoft Windows 7 Professional ","6.1.7601"

The script look like this. What have i done wrong?

Const ForWriting = 2, ForAppending = 8

strFile = "V:\UDvikling\objOperatingSystem.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
	Set objLogFile = objFSO.OpenTextFile(strFile, _ 
    ForAppending, True)
Else
    Wscript.Echo "File does not exist."
	Set objLogFile = objFSO.CreateTextFile(strFile, _ 
    ForAppending, True)
End If

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
    objLogFile.Write chr(34) & objOperatingSystem.Caption  & chr(34) & "," 
    objLogFile.Write chr(34) & objOperatingSystem.Version  & chr(34) 
    objLogFile.Writeline
Next

objLogFile.Close

March 31st, 2015 2:32pm

Try it like this.  If you still get bad characters then you have a system issue.

Const ForWriting = 2, ForAppending = 8

strFile = "V:\UDvikling\objOperatingSystem.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objLogFile = objFSO.OpenTextFile(strFile, ForAppending,True)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    objLogFile.Write chr(34) & objOperatingSystem.Caption  & chr(34) & "," 
    objLogFile.Write chr(34) & objOperatingSystem.Version  & chr(34) 
    objLogFile.Writeline
Next

objLogFile.Close
Manually delete the old log file before you start testing.
Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 3:51pm

I reviewed my script and found the error...was missing the format

object.OpenTextFile(filename[, iomode[, create[, format]]])

The below code work as it should now. No Chinese stuff there hehe.

If objFSO.FileExists(strFile) Then
	'Wscript.Echo "File exist."
	Set objLogFile = objFSO.OpenTextFile(strFile, ForAppending, False, True)
Else
    'Wscript.Echo "File does not exist."
	Set objLogFile = objFSO.CreateTextFile(strFile, ForWriting, True)
End If

I created the file in unicode but open appended to it as ANCII.

But your code is must simpler and take less bytes so i will go with that thank you,

April 1st, 2015 1:50am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics