Kwag_Myers
Kwag_Myers

Reputation: 53

VBS slows near the end

I have this script I use to compress a copy of my files to a network drive. The Processing dialog shows the files being copied and I've noticed the process seems to go through files really fast at first, but then slows way down for the last 20% or so (judging by the progress bar). I've noticed this on both W7 32 and 64 bit. Some text files that are only a few K may take a minute or two.

Is this normal, or is there something in my script might be causing the slow-down?

'Target directory
ZipFile = "Z:\MyDocsBU\MyDocsBackup_" & Right("0" & DatePart("m",Now()),2) & Right("0" & DatePart("d",Now()),2) & DatePart("yyyy",Now()) & ".zip"

'Check for source folder on file
set filesys = CreateObject("Scripting.FileSystemObject")

If filesys.FileExists("Z:\MyDocsBU\SourceFolder.txt") Then
    set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("z:\MyDocsBU\SourceFolder.txt", 1, true)
    setDirectory = objFileToRead.ReadAll()
    objFileToRead.Close
    Set objFileToRead = Nothing
    SourceFolder = InputBox("You are about to back up:", "Source Folder", setDirectory)

Else
    'Source directory with user input first time only
    Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("Z:\MyDocsBU\SourceFolder.txt",2,true)

    SourceFolder = InputBox("Please enter the folder directory to back up." & vbCrLf & vbCrLf & "Example:" & vbCrLf & "C:\Users\your.name\Documents", "Source Folder", "C:\Users\")
    If SourceFolder = "" Then
        Wscript.Quit
    Else
        objFileToWrite.Write(SourceFolder)
        objFileToWrite.close
    End If

End If

Const FOF_CREATEPROGRESSDLG = &H0&

' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)

' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items

' Add all files/directories to the .zip file and show progress bar
zip.CopyHere(sourceItems), FOF_CREATEPROGRESSDLG

'Wait for items to be copied, hides behind progress bar
wscript.echo "Wait until progress bar closes before clicking OK."

Upvotes: 1

Views: 622

Answers (1)

Syberdoor
Syberdoor

Reputation: 2619

This is probably due to write caching. Windows buffers the files into memory first and then to the target destination from there. The main benefit of this is that if an application waits on the file write it gets notified earlier and can continue. If the target drive is a lot slower in writing than the source is in reading in windows 7 this will lead to the behaviour you described. Normally the files are copied with a speed much higher than technically possible at the beginning (200MB/s to USB 2.0 drives etc). The progress bar is based on the total amount of data copied so the gains will be huge at the beginning. As the time the copy job takes is not really improved by this method the slowdown in the end once the cache is filled is inevitable.

You can easily check if your script is at fault by just manually starting the same copy but as you use the windows explorer file copy anyway I doubt that anything in your script is at fault here.

Upvotes: 0

Related Questions