locked
Generate a log file to a script! RRS feed

  • Question

  • Hey all;

    I have the following script, which I want to add logging capabilities to it, the script does copy a file from a computer to multiple computers on the network, so I want a log file that contains the result of the copy & replace operation if successful or not.


    Const ForReading = 1
    Const OverwriteExisting = TRUE

    'setting objects
    Dim net, objFSO, shell
    Dim objFile, strLine, intResult
    Set objnet = CreateObject("wscript.network")
    Set objFSO = CreateObject("scripting.filesystemobject")
    Set objshell = CreateObject("wscript.shell")
    Set objFile = objFSO.OpenTextFile("d:\TEST\list.txt")
    strfile = "d:\TEST\Q2000.qst"
    strUser = "domain\user"
    strPassword = "password"

    'Check if source file exists
    If Not objFSO.FileExists(strFile) Then
    WScript.Echo "The source file does not exist"
    WScript.Quit
    End If

    Do Until objFile.AtEndOfStream
    strComputer = objFile.ReadLine
    strRemoteFile = "\\" & strComputer & "\C$\Temp\Q2000.qst"
    objFSO.CopyFile strfile, strRemoteFile, OverwriteExisting
    Loop


    Please help.

    Thanks.

    Wednesday, June 17, 2009 6:35 AM

Answers

  • Open the file you want to log results to using the objFSO.OpenTextFile method and use one of the iomodes ForWriting or ForAppending depending on if you want to start a new file or append results to an existing file.

    Add the following to open a logfile

    Const ForWriting = 2 ' Open a file for writing
    Const ForAppending = 8 ' Open a file and write to the end of the file
    Set objResultFile = objFSO.OpenTextFile("d:\TEST\results.txt", ForWriting, True)


    To modify your code to log results you could do something like this:

    'Check if source file exists
    If Not objFSO.FileExists(strFile) Then
        objResultFile.WriteLine "The source file does not exist"
        WScript.Quit
    End If

    On Error Resume Next
    Do Until objFile.AtEndOfStream
        strComputer = objFile.ReadLine
        strRemoteFile = "\\" & strComputer & "\C$\Temp\Q2000.qst"
        err.Clear
        objFSO.CopyFile strfile, strRemoteFile, OverwriteExisting
        If err.Number = 0 Then
            objResultFile.WriteLine strRemoteFile & " copied successfully"
        Else
            objResultFile.WriteLine strRemoteFile & " couldn't be copied. Error " & err.Number & " " & err.Description
        End If
    Loop

    You can also use a Select Case statement to use different loggin per error number if you know which errors you want to treat.

    Select Case err.Number
    Case 0
        ...
    Case 5
        ...
    Case Else
        ...
    End Case

    I hope my examples work. I haven't been able to try them. They should at least give you an idea about how to approach it.
    let me know if you need more help.

    • Proposed as answer by Salvador Manaois III Friday, August 14, 2009 8:03 AM
    • Marked as answer by IamMred Friday, January 8, 2010 12:27 AM
    Wednesday, June 17, 2009 10:10 AM
  • I would suggest using the robust logging capabilities of the ROBOCOPY tool.

    http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx

    Regards,

    Salvador Manaois III
    MCITP | Enterprise & Server Administrator
    MCSE MCSA MCTS(x5) CIWA C|EH
    My Blog: Bytes and Badz 
    My Passion: View My PhotoStream
    • Proposed as answer by Salvador Manaois III Friday, August 14, 2009 8:03 AM
    • Marked as answer by IamMred Friday, January 8, 2010 12:28 AM
    Thursday, June 18, 2009 8:58 AM

All replies

  • Open the file you want to log results to using the objFSO.OpenTextFile method and use one of the iomodes ForWriting or ForAppending depending on if you want to start a new file or append results to an existing file.

    Add the following to open a logfile

    Const ForWriting = 2 ' Open a file for writing
    Const ForAppending = 8 ' Open a file and write to the end of the file
    Set objResultFile = objFSO.OpenTextFile("d:\TEST\results.txt", ForWriting, True)


    To modify your code to log results you could do something like this:

    'Check if source file exists
    If Not objFSO.FileExists(strFile) Then
        objResultFile.WriteLine "The source file does not exist"
        WScript.Quit
    End If

    On Error Resume Next
    Do Until objFile.AtEndOfStream
        strComputer = objFile.ReadLine
        strRemoteFile = "\\" & strComputer & "\C$\Temp\Q2000.qst"
        err.Clear
        objFSO.CopyFile strfile, strRemoteFile, OverwriteExisting
        If err.Number = 0 Then
            objResultFile.WriteLine strRemoteFile & " copied successfully"
        Else
            objResultFile.WriteLine strRemoteFile & " couldn't be copied. Error " & err.Number & " " & err.Description
        End If
    Loop

    You can also use a Select Case statement to use different loggin per error number if you know which errors you want to treat.

    Select Case err.Number
    Case 0
        ...
    Case 5
        ...
    Case Else
        ...
    End Case

    I hope my examples work. I haven't been able to try them. They should at least give you an idea about how to approach it.
    let me know if you need more help.

    • Proposed as answer by Salvador Manaois III Friday, August 14, 2009 8:03 AM
    • Marked as answer by IamMred Friday, January 8, 2010 12:27 AM
    Wednesday, June 17, 2009 10:10 AM
  • I would suggest using the robust logging capabilities of the ROBOCOPY tool.

    http://technet.microsoft.com/en-us/library/cc733145(WS.10).aspx

    Regards,

    Salvador Manaois III
    MCITP | Enterprise & Server Administrator
    MCSE MCSA MCTS(x5) CIWA C|EH
    My Blog: Bytes and Badz 
    My Passion: View My PhotoStream
    • Proposed as answer by Salvador Manaois III Friday, August 14, 2009 8:03 AM
    • Marked as answer by IamMred Friday, January 8, 2010 12:28 AM
    Thursday, June 18, 2009 8:58 AM