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