我在老式学校里使用VBScript自动化日常工作,并尝试编写代码以显示备份时的进度百分比。我使用Robocopy及其日志来计算所附加代码的百分比。
问题在于它不会重复,因为我认为正在更新的log.txt文件已锁定?
objFSO.CopyFile "C:\temp\log.txt", "C:\temp2\"
我认为这不是实现目标的好方法,但这就是我能想到的。 如果有更好的方法,谢谢您的分享。
谢谢。
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