使用VBScript压缩文件而不睡觉

时间:2014-07-24 06:14:17

标签: windows vbscript zipfile

我想知道是否有使用vbscript压缩文件而不使用WScript.Sleep。以下是我的脚本(跳过实用程序方法);

Sub Main
   Dim Path
   Dim ZipFile
   Dim objShell

   ZipFile = WScript.Arguments(0)
   Path = WScript.Arguments(1)

   Dim a: a = ListDir(Path)
   If UBound(a) = -1 then
      WScript.Echo "No files found."
      Exit Sub
      End If

   CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
   Set objShell = CreateObject("Shell.Application")
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   Dim FileName
   For Each FileName In a
      WScript.Echo FileName
      objShell.NameSpace(fso.GetAbsolutePathName(ZipFile)).CopyHere(FileName)
      'Required!
      WScript.Sleep 4000
      Next
   End Sub

如果你看到代码,我正在使用sleep命令等待一段时间(假设文件在指定时间内被压缩)。但是,这导致脚本即使对于不好的小文件也要等待同一时间。我在线搜索是否有同步压缩文件,但无法得到答案。

2 个答案:

答案 0 :(得分:1)

Shell.NameSpace操作是异步的。据我所知,从vbscript,不可能完全删除Sleep,因为至少需要等待异步进程启动,但是,你可以尝试访问zip文件以了解操作是否已结束。 (对不起,我已经重新编写了测试代码)

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

WaitForFile函数将返回一个布尔值,指示文件是否已在指定的超时内变为可写(没有锁定文件的操作)。示例代码使用-1作为超时,即等待文件可写。当NameSpace操作结束(源文件已压缩)时,zip文件中将没有锁定,函数将返回。

答案 1 :(得分:0)

对我来说,你可以用一个循环替换当前的睡眠,比较ZipFile中的文件数和源文件夹中的文件数。如果一个小于另一个,请做一个短暂的睡眠。要使其工作,您需要将a修改为FSO文件夹对象而不是文件名数组。然后你可以使用它的.Count属性。 Like This