Comment déterminer quand la copie se termine dans VBScript?
-
02-07-2019 - |
Question
Est-ce que quelqu'un connaît une méthode pour déterminer le moment où une copie de fichier se termine dans VBScript? J'utilise les éléments suivants pour copier:
set sa = CreateObject("Shell.Application")
set zip = sa.NameSpace(saveFile)
set Fol = sa.NameSpace(folderToZip)
zip.copyHere (Fol.items)
La solution
Do Until zip.Items.Count = Fol.Items.Count
WScript.Sleep 300
Loop
Une fois la boucle terminée, votre copie est terminée.
Mais si vous voulez seulement copier et non zip, FSO ou WMI est préférable.
Si vous zippez et que vous les voulez dans un fichier, vous devez créer le fichier zip vous-même, avec le bon en-tête en premier. Sinon, vous ne recevez que des fichiers / dossiers compressés IIRC. Quelque chose comme ça:
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
Le 2 dans OpenTextFile est ForWriting.
Autres conseils
Vous pouvez avoir plus de chance en utilisant la méthode Copy sur un FileSystemObject
. Je l'ai utilisé pour la copie et c'est un appel bloquant.
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