简体   繁体   English

VBS快快结束了

[英]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). “处理”对话框显示了正在复制的文件,我注意到该过程似乎一开始确实非常快,但是在最后20%左右(根据进度栏判断)之后,速度却变慢了。 I've noticed this on both W7 32 and 64 bit. 我在W7 32和64位上都注意到了这一点。 Some text files that are only a few K may take a minute or two. 一些只有几K的文本文件可能需要一两分钟。

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."

This is probably due to write caching. 这可能是由于写入缓存。 Windows buffers the files into memory first and then to the target destination from there. Windows先将文件缓冲到内存中,然后再从那里缓冲到目标位置。 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. 如果目标驱动器的写入速度比源驱动器在Windows 7中的读取速度慢得多,这将导致您描述的行为。 Normally the files are copied with a speed much higher than technically possible at the beginning (200MB/s to USB 2.0 drives etc). 通常,文件的复制速度比开始时的技术速度要高得多(200MB / s到USB 2.0驱动器等)。 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. 您可以通过手动启动相同的副本来轻松检查脚本是否有错误,但是无论如何在您使用Windows资源管理器文件副本时,我都怀疑脚本中的任何内容都存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM