调用vbscript的Windows批处理文件不会等待它完成

时间:2015-03-03 16:47:45

标签: .net windows batch-file vbscript

我有一个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%

2 个答案:

答案 0 :(得分:2)

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 

答案 1 :(得分:0)

这是我写的一个可以帮助你的函数:

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