如何在VBScript中确定复制何时完成?

4
有没有人知道在VBScript中确定文件复制完成的方法?我正在使用以下代码进行复制:
set sa = CreateObject("Shell.Application")  
set zip = sa.NameSpace(saveFile)  
set Fol = sa.NameSpace(folderToZip)  
zip.copyHere (Fol.items)
3个回答

6
Do Until zip.Items.Count = Fol.Items.Count
    WScript.Sleep 300
Loop

循环完成后,您的复制品就完成了。
但如果您只想复制而不压缩,则使用FSO或WMI更好。
如果要进行压缩并希望将它们保存在文件中,则必须先自己创建正确的标头zip文件。否则,您只会得到已压缩的文件/文件夹(如果我没记错的话)。类似于以下内容:
Set FSO = CreateObject( "Scripting.FileSystemObject" )
Set File = FSO.OpenTextFile( saveFile, 2, True )
File.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
File.Close
Set File = Nothing
Set FSO = Nothing

OpenTextFile中的2表示ForWriting。

可能会进行压缩,因此不使用FSO。 - tloach

2

您可能会更幸运地使用FileSystemObject上的复制方法。我已经用它来复制,它是一个阻塞调用。


0
Const FOF_CREATEPROGRESSDLG = &H0&
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")

strSource = " " ' Source folder path of log files
strTarget = " .zip" ' backup path where file will be created

AddFilesToZip strSource,strTarget

Function AddFilesToZip (strSource,strTarget)
Set r=fso.GetFolder(strSource)
    set file = fso.opentextfile(strTarget,ForWriting,true) 
    file.write "PK" & chr(5) & chr(6) & string(18,chr(0)) 
    file.Close
    Set shl = CreateObject("Shell.Application")
    i = 0

        For each f in r.Files
            If fso.GetExtensionName(f) = "log" Or fso.GetExtensionName(f) = "Log" Or fso.GetExtensionName(f) = "LOG" Then
            shl.namespace(strTarget).copyhere(f.Path)', FOF_CREATEPROGRESSDLG   
                Do until shl.namespace(strTarget).items.count = i
                    wscript.sleep 300
                Loop
            End If
            i = i + 1
        Next

        set shl = Nothing
End Function

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接