0

我想将文件添加到文件夹并将其保存为 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
4

1 回答 1

2

显示在创建新的 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

(未经测试)

于 2013-01-22T19:06:42.347 回答