none
VBS script append to csv file - wierd behavior RRS feed

  • Question

  • 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
    


    Helpdesk Supporter

    Tuesday, March 31, 2015 6:29 PM

Answers

  • 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.


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, March 31, 2015 7:48 PM
    • Marked as answer by LongQuo Wednesday, April 1, 2015 5:47 AM
    Tuesday, March 31, 2015 7:47 PM

All replies

  • 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.


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, March 31, 2015 7:48 PM
    • Marked as answer by LongQuo Wednesday, April 1, 2015 5:47 AM
    Tuesday, March 31, 2015 7:47 PM
  • 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,


    Helpdesk Supporter

    Wednesday, April 1, 2015 5:47 AM