为什么这个VBScript没有将文件添加到ZIP文件中?

时间:2012-12-05 12:52:21

标签: vbscript zip

我想将文件添加到文件夹并将其保存为VBScript中的压缩文件夹。

我编写了以下代码,但它只创建了ZIP文件而没有添加文件。这段代码有什么问题?

Option Explicit
dim wshShell
Const MoveMode = True
Const BackupDir = "D:\csv\Image\"
Const Outfilename = "MyZip.zip"
Const TimeoutMins = 10 ' Timeout for individual file compression operation
'Set wshShell = CreateObject("WScript.shell")
Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject")
Dim Folder : Set Folder = FSO.GetFolder("D:\csv\Image")
Dim Files : Set Files = Folder.Files

Dim File
Dim Counter : Counter=0
Dim Timeout : Timeout = 0

FSO.CreateTextFile "D:\csv\" & OutFilename,true '.WriteLine "PK" & Chr(5) & Chr(6) &             String(18, 0)

Dim Shell : Set Shell = CreateObject("Shell.Application")
Dim ZipFile: Set ZipFile = Shell.NameSpace("D:\csv\"& OutFilename)

If Not ZipFile Is Nothing Then 
  Shell.NameSpace("D:\csv\"&Outfilename).CopyHere "D:\csv\Image\calender.png"
End If

1 个答案:

答案 0 :(得分:1)

This显示在创建新的ZIP文件之后等待500毫秒,然后再尝试将数据复制到其中。您是否尝试过使用WScript.Sleep(500)?

Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject")
Dim Shell : Set Shell = CreateObject("Shell.Application")

If Not FSO.FileExists("D:\csv\" & OutFilename) Then
  NewZip("D:\csv\" & OutFilename)
End If

If Not ZipFile Is Nothing Then 
  Shell.NameSpace("D:\csv\" & Outfilename).CopyHere BackupDir & "calender.png"
End If

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

(未测试的)