0

I am old school to automate routine jobs using VBScript and trying to code to show progress percentage while backup. I use Robocopy and its logs to calculate percentage as the code attached.

The problem is that it doesn't duplicate because I think the log.txt file being updated is locked?

objFSO.CopyFile "C:\temp\log.txt", "C:\temp2\"

I don't think this is a good way to accomplish it but this is all I can think of. If there is a better way, I appreciate if you share.

Thanks in advance.

On Error Resume Next 
strComputer = "." 
strProcessToFind1 = "Robocopy.exe"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
totalToCopy = "C:\temp\totalToCopy.txt"

objShell.Run "Robocopy.exe C:\sourceFolder C:\destinationFolder /mir /l /log:" & totalToCopy & " /bytes /njh /nc /nfl /ndl /np", 0, True 
Dim arrFileLines()
i = 0
Set objFile = objFSO.OpenTextFile(totalToCopy, 1)
Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFile.ReadLine
     i = i + 1
Loop
objFile.Close

j = 0
For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
    j = j + 1
    If j = 4 Then ' find the total size of copy. always in 4th line of the file from bottom.
        totalSize = Replace(arrFileLines(l), "   Bytes : ", "")
        findSpace = InStr(1, totalSize, " ", 1)
        totalSize = Left(totalSize, findSpace - 1)
        Exit For 
    Else
    End If 
Next    

'================================================================================================

objShell.Run "Robocopy.exe C:\sourceFolder C:\destinationFolder /mir /log:C:\temp\log.txt /bytes /ndl /nc /np /njh", 0
sQuery = ("Select * from Win32_Process where name ='" & strProcessToFind1 & "'")
Do 
    objFSO.CopyFile "C:\temp\log.txt", "C:\temp2\"
    Set objFile = objFSO.OpenTextFile("C:\temp2\log.txt")
    sum = 0
    Do Until objFile.AtEndOfStream
        strTmp = objFile.ReadLine
        If strTmp = "" Then
        Else 
            strTmp = Replace(strTmp, " ", "")
            strTmp = Replace(strTmp, "  ", "") 
            findChar = InStr(1, strTmp, "C", 1)
            strTmp = Left(strTmp, findChar - 1)
            strTmp = CInt(strTmp)
            sum = sum + strTmp
        End If 
    Loop
    currentCopiedSize = CInt((sum / totalSize) * 100)
    WScript.Echo currentCopiedSize & "%"
    objFile.Close

    If objWMIService.ExecQuery(sQuery).Count > 0 then
    Else
        Exit Do                 
    End If  
    WScript.Sleep 3000      
Loop 
Charles
  • 9
  • 2
  • What happens when you run this? I would expect the `objShell.Run "Robocopy.exe..."` call does not return until it is done so you wouldn't see anything happen in your Do loop afterwards. – Étienne Laneville Nov 03 '19 at 23:42
  • The first robocopy, line 9, returns when it is done but the second robocopy just run and doesn't wait for return because it doesn't have "true" parameter at the end of the robocopy command. – Charles Nov 03 '19 at 23:51
  • Have you tried using ForReading parameter on your `objFSO.OpenTextFile`? `Set objFile = objFSO.OpenTextFile("C:\temp2\log.txt", 1)`. You might be able to simply read the log file directly without copying it. – Étienne Laneville Nov 04 '19 at 00:07
  • 1
    It's still quiet unclear what the issue is. If this script functions correctly, please explain how you want to improve it. If it doesn't function correctly, please explain why. My first response to "can this be done better" is "Yes - use Powershell" https://stackoverflow.com/questions/2434133/progress-during-large-file-copy-copy-item-write-progress – Nick.Mc Nov 04 '19 at 00:08
  • 1
    I would suggest copying the files using FileSystemObject, that way you can track progress yourself (kb & # of files left) – Étienne Laneville Nov 04 '19 at 02:50
  • @ Étienne Laneville, Thanks for your suggestion. VBScript doesn't copy nor read a text file being updated. In your last comment using FileSystemObject, does this mean that I don't use robocopy? – Charles Nov 05 '19 at 00:06
  • @Charles yes, last comment was suggesting copying the files without robocopy but you must have reasons for using it. As far as reading a file being updated, I wanted to make sure you tried it with the ForReading flag.The `Set objFile = objFSO.OpenTextFile("C:\temp2\log.txt")` line doesn't use an IO parameter but I understand it is expecting a copy of the file at that point. – Étienne Laneville Nov 05 '19 at 23:53
  • I tried using ForReading flag but it doesn't work but I used Hobocopy for volume shodow copy utility to copy locked file and successfully calculate progress percentage. Even though it works with Hobocopy, it uses high system resource and may cause system unstable. Thanks for your advice but it is good idea to study other solutions in the comments. Thanks all. – Charles Nov 06 '19 at 07:40

0 Answers0