Shell.NameSpace
operations are asynchronous. And as far as i know, from vbscript, it is not possible to completely remove the Sleep
, as it is necessary to at least wait for the asynchonous process to start, BUT, you can try to get access to the zip file to know if the operation has ended. (sorry, i have rewritted your code for testing)
Option Explicit
Sub Main
' Retrieve arguments
Dim strPath, strZipFile
If Wscript.Arguments.UnNamed.Count < 2 Then
WScript.Echo "No arguments"
Exit Sub
End If
strZipFile = WScript.Arguments(0)
strPath = WScript.Arguments(1)
' Create needed objects
Dim fso, shell
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set shell = WScript.CreateObject("Shell.Application")
' Check for valid source path
If Not fso.FolderExists( strPath ) Then
WScript.Echo "Folder does not exist"
Exit Sub
End If
Dim oFolder
Set oFolder = fso.GetFolder( strPath )
If oFolder.Files.Count < 1 Then
WScript.Echo "No files found"
Exit Sub
End If
' Initialize zip file access
Dim oZipFile
strZipFile = fso.GetAbsolutePathName( strZipFile )
fso.CreateTextFile( strZipFile, True ).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set oZipFile = shell.NameSpace( strZipFile )
' Add files to zip
Dim oFile
For Each oFile In oFolder.Files
WScript.Echo oFile.Name
oZipFile.CopyHere(oFile.Path)
WScript.Sleep 500
WaitForFile strZipFile, -1
Next
End Sub
' Wait for a file to become writeable
Function WaitForFile( FullPathToFile, TimeToWait )
Dim fso, timeLimit, oFile
WaitForFile = False
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' Determine if we are going to wait for the file
If TimeToWait > 0 Then
timeLimit = DateAdd("s", TimeToWait, Now )
ElseIf TimeToWait = 0 Then
timeLimit = Now
Else
timeLimit = DateAdd("yyyy", 100, Now)
End If
' Loop until the file can be written or the timeout has been reached
On Error Resume Next
Do
Err.Clear
Set oFile = fso.OpenTextFile( FullPathToFile, 8, False )
If Err.Number = 0 Then
oFile.Close
WaitForFile = True
Exit Do
End If
WScript.Sleep 100
Loop While Now < timeLimit
On Error GoTo 0
End Function
' Call main process
Main
The WaitForFile
function will return a Boolean indicating if the file has become writeable (there is no operation locking the file) in the indicated timeout. Sample code uses -1
as timeout, that is, wait until the file is writeable. When the NameSpace operation has ended (the source file has been zipped), there will be no locks in the zip file and the function will return.