none
Help with Robocopy in a VBScript, log file output issue RRS feed

  • Question

  • In a VBSCRIPT, I am sending a robocopy command to the shell, but the resulting log file is messy. The same command issued at the command prompt gives a clean output. I am wondering if this is normal or if there is something that I can do to correct this.

    My script goes like this:

    Option Explicit
    On Error Resume Next

    Dim strComputer, strServrPath, strBackupUserID, strDestinationPath, fso
    Dim strPCSourcePath, objShell, objFile

    strComputer = "NCRWKIT107012"
    strServrPath = "server_shared_folder"
    strBackupUserID = "CADIEUP"
    strDestinationPath = strServrPath & strBackupUserID & "\"
    strPCSourcePath = usrHomeDir() & "\"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objShell = WScript.CreateObject("WScript.Shell")
    Set objFile = fso.CreateTextFile(UCase(strDestinationPath & "log.txt"), True, True)


    objFile.WriteLine(UCase("Computer Name: " & strComputer))
    objFile.WriteLine(UCase("User: " & strBackupUserID))

    ' Section: English User
    objFile.WriteLine("")
    objFile.WriteLine("Files from the Client Folder")
    objFile.WriteLine("")

    ' Pictures
    Dim strShellCmdEU06
    strShellCmdEU06 = ("robocopy.exe " & strPCSourcePath & "Pictures\" & " "  & strDestinationPath & "Pictures\"  & " /s /XJ /r:1 /w:2 /fp /tee /np /v /x /xa:SH /LOG+:" & strDestinationPath & "LOG1.txt")

    ' \Pictures
    objFile.WriteLine(strShellCmdEU06)
    objShell.Run strShellCmdEU06, 1, True

    objFile.WriteLine "Script Completed Successfully"
    objFile.Close

    ' Clean up
    Set fso = Nothing
    Set objShell = Nothing
    Set objFile = Nothing
    MsgBox "Done."
    wscript.quit

    REM '#############################
    REM '# Functions and SubRoutines #
    REM '#############################

    Function usrHomeDir()
     'This function returns a STRING of the user's home directory eg: C:\Documents and Settings\CADIEUP or C:\users\CADIEUP
     Dim WshShell, WshSysEnv
     Set WshShell = CreateObject("WScript.Shell")
     Set WshSysEnv = WshShell.Environment("PROCESS")
     usrHomeDir = WshSysEnv("USERPROFILE")
     Set WshShell = Nothing
     Set WshSysEnv = Nothing
    End Function

    The expected file output is like this:

    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows                             
    -------------------------------------------------------------------------------

      Started : Wed Aug 13 10:50:26 2014

       Source : C:\Users\cadieup\Pictures\
         Dest : server_shared_folder\CADIEUP\Pictures\

        Files : *.*
        
      Options : *.* /V /X /FP /TEE /S /COPY:DAT /NP /XJ /XA:SH /R:1 /W:2

    ------------------------------------------------------------------------------

                       12 C:\Users\cadieup\Pictures\
             attrib       504 C:\Users\cadieup\Pictures\desktop.ini
             attrib     72192 C:\Users\cadieup\Pictures\Thumbs.db
    ...

    REMOVED CONTENT

    ...

               same     1.5 m C:\Users\cadieup\Pictures\2013-07-21_AfterTheStorm\IMG_0053.JPG
       New Dir          0 C:\Users\cadieup\Pictures\BlackBerry\

    ------------------------------------------------------------------------------

                   Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :         3         1         2         0         0         0
       Files :        44         0        44         0         0         0
       Bytes :   98.26 m         0   98.26 m         0         0         0
       Times :   0:00:00   0:00:00                       0:00:00   0:00:00

       Ended : Wed Aug 13 10:50:26 2014

    The actual result file output looks like this:

    C:\Users\cadieup\Pictures\"server_shared_folder"\CADIEUP\Pictures\*.*
         *.* /V /X /FP /TEE /S /COPY:DAT /NP /XJ /XA:SH /R:1 /W:2 C:\Users\cadieup\Pictures\C:\Users\cadieup\Pictures\desktop.iniC:\Users\cadieup\Pictures\Thumbs.db

    ...

    REMOVED CONTENT

    ...

    C:\Users\cadieup\Pictures\2013-07-21_AfterTheStorm\IMG_0053.JPGC:\Users\cadieup\Pictures\BlackBerry\                                       3       3       0 0 0 0       44      41       3 0 0 0  98.26 m 98.15 m 117.4 k 0 0 0                                             

    Wednesday, August 13, 2014 3:58 PM

All replies

  • Use the same font as the command prompt.

    David Candy

    Saturday, August 16, 2014 9:59 PM
  • There are a couple of oddities here, possibly side effects of troubleshooting.

    Here's what I think the core problem is: objFile is a handle to the file's textstream, and locks the file until you release it. This prevents Robocopy from writing to it.

    To fix this problem, before you run your robocopy command, insert the statement

    objFile.Close

    The object goes away at this point, so after Robocopy finishes you will need to reopen the file using the OpenTextFile method.


    The other issues I see, some of which may just be testing artifacts:

    (1) premature error suppression
    You should comment out the On Error Resume Next statement. It's useful for production script use sometimes, but in this case all it does is neutralize your Option Explicit statement and prevent you from seeing true script errors.

    (2) multiple log files
    The script writes to 2 different files, log.txt and log1.txt. Did you mean to do this?
    Also, make sure you delete the log files before each test, since you're appending - forgetting to do that has bitten me before. You may want to insert a file deletion statement in the script itself - use FileExists(filepath)

    (3) Unicode/ASCII
    You create a Unicode file with
           fso.CreateTextFile(UCase(strDestinationPath & "log.txt"), True, True)
    but aren't telling Robocopy to use Unicode. Change the /LOG+ to /UNILOG+ in the robocopy command.

    In any case, I had no issues with getting robocopy to log to the same file when I closed the file first. Let us know how this goes!

    Alex

    Monday, August 18, 2014 6:14 PM
  • Thank you Alex,

    I tried the UNILOG, that did not help. 2 Log files is intended, 1 holds the command issued and the second holds the log of the actual files that were backed up. As for the error suppression, I did not realize that it nulled the "Option Explicit", I just always use both in my scripts and comment out one or the other to troubleshoot.

    Getting robocopy to create and populate the "LOG1" file is not the source of my issue, it is with the output format. The files are listed consecutively without the line break and formatting I expected. When executed from the script, robocopy to just dump all the file names consecutively like:

    file1file2file3...Summary

    When executed from the command prompt, robocopy logs each file on a 1 per line like:

    file1

    file2

    file3

    ...

    Summary

    Monday, August 18, 2014 6:46 PM
  • So this IS the LOG1.TXT file that shows run-together lines?

    Using your code as my starting point, I'm not able to replicate your results so far. I get lines like this:

    	  *EXTRA File 		   6.9 m	C:\temp\2014-08-07-2.ab
    	  *EXTRA File 		   6.9 m	C:\temp\2014-08-07-2.zip
    	  *EXTRA File 		    5059	C:\temp\2014042812230144200281357.ics
    

    I know it can't be the LOG.TXT file - your demo code never captures and writes file names to it.

    Tuesday, August 19, 2014 4:15 PM