【问题标题】:Windows batch file calling vbscript doesn't wait for it finish调用 vbscript 的 Windows 批处理文件不会等待它完成
【发布时间】:2015-03-03 16:47:45
【问题描述】:

我有一个用于压缩一些文件的 VBScript。从批处理文件调用时,批处理不会等待脚本完成,并且您似乎需要在 vbscript 中休眠以给压缩足够的时间,否则您最终会得到一个空的或损坏的 zip 文件。有什么办法可以让cscript执行同步吗?

以下是脚本:

Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 30000

请注意最后的睡眠,这是必要的,但很糟糕,因为我不知道实际的压缩需要多长时间。以及批处理文件调用:

CScript //B zipCOM.vbs %TEMPZIPDIR% %ARCHIVEFILE%

【问题讨论】:

    标签: .net windows batch-file vbscript


    【解决方案1】:

    zip 文件夹操作是异步的。您需要等待代码结束。这是一种简单的方法:等到文件上的锁定(通过压缩操作保持)被释放。

    Option Explicit
    
    Dim objArgs, InputFolder, ZipFile
        Set objArgs = WScript.Arguments
        InputFolder = objArgs.Item(0)
        ZipFile = objArgs.Item(1)
    
    Dim fso
        Set fso = WScript.CreateObject("Scripting.FileSystemObject")
        fso.CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
    
    Dim objShell, source
        Set objShell = CreateObject("Shell.Application")
        Set Source = objShell.NameSpace(InputFolder).Items
    
        objShell.NameSpace(ZipFile).CopyHere( source )
    
        ' Wait for the operation to start
        WScript.Sleep 3000
    
        ' Loop until the file can be written 
    Dim objFile
        On Error Resume Next
        Do While True
            Err.Clear
            Set objFile = fso.OpenTextFile( ZipFile, 8, False )
            If Err.Number = 0 Then
                objFile.Close
                Exit Do
            End If
            WScript.Sleep 100
        Loop 
    

    【讨论】:

      【解决方案2】:

      这是我写的一个可能对你有帮助的函数:

      http://www.naterice.com/blog/template_permalink.asp?id=64

      Function WindowsZip(sFile, sZipFile)
        'This script is provided under the Creative Commons license located
        'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
        'be used for commercial purposes with out the expressed written consent
        'of NateRice.com
      
        Set oZipShell = CreateObject("WScript.Shell")  
        Set oZipFSO = CreateObject("Scripting.FileSystemObject")
      
        If Not oZipFSO.FileExists(sZipFile) Then
          NewZip(sZipFile)
        End If
      
        Set oZipApp = CreateObject("Shell.Application")
      
        sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
      
        aFileName = Split(sFile, "\")
        sFileName = (aFileName(Ubound(aFileName)))
      
        'listfiles
        sDupe = False
        For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
          If LCase(sFileName) = LCase(sFileNameInZip) Then
            sDupe = True
            Exit For
          End If
        Next
      
        If Not sDupe Then
          oZipApp.NameSpace(sZipFile).Copyhere sFile
      
          'Keep script waiting until Compressing is done
          On Error Resume Next
          sLoop = 0
          Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
            Wscript.Sleep(100)
            sLoop = sLoop + 1
          Loop
          On Error GoTo 0
        End If
      End Function
      
      Sub NewZip(sNewZip)
        'This script is provided under the Creative Commons license located
        'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
        'be used for commercial purposes with out the expressed written consent
        'of NateRice.com
      
        Set oNewZipFSO = CreateObject("Scripting.FileSystemObject")
        Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)
      
        oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
      
        oNewZipFile.Close
        Set oNewZipFSO = Nothing
      
        Wscript.Sleep(500)
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2013-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多