使用 VB 脚本压缩文件而无需睡眠 [英] Zip files using VB Script with out sleep

查看:28
本文介绍了使用 VB 脚本压缩文件而无需睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to know if there is anyway of zipping files using vbscript without using WScript.Sleep. Following is my script(Utility methods are skipped);

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 命令等待特定时间(假设文件在指定时间内被压缩).但是,这导致脚本即使对于不好的小文件也要等待相同的时间.我在网上搜索过是否有同步压缩文件,但无法得到答案.

If you see the code, I'm using sleep command to wait for certain time(Assuming that the files gets zipped in the specified time). However, this is causing the script to wait for the same time even for the small files which is not good. I've searched online if there is anyway of zipping files synchronously, but couldn't get the answer.

推荐答案

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

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

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

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.

这篇关于使用 VB 脚本压缩文件而无需睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆