为什么';此VBScript是否将文件添加到ZIP文件?

为什么';此VBScript是否将文件添加到ZIP文件?,vbscript,zip,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

我想将文件添加到文件夹中,并将其另存为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
显示创建新ZIP文件后等待500毫秒,然后再尝试将数据复制到其中。你试过使用WScript.Sleep(500)吗


(未测试)

您能告诉我们您已经有了什么吗?我认为
CopyHere()
的可能重复是异步的,并且(如果这是您的完整代码列表)您的脚本过程在复制任何文件之前结束。@shiplu.mokadd.im:这个问题不是重复的;它问为什么特定的代码不起作用。你能简化你的代码,使它不包含任何与问题无关的东西(变量、语句等)吗?是的,我写了这个。文件系统通常需要一秒钟左右的时间来写入文件,因此我在其中暂停。我认为现代SSD可能不再需要这种暂停。
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