0

I want to add a file to a folder and save it as a compressed folder in VBScript.

I've written the following code, but it only creates the ZIP file and doesn't add the files to it. What can be the problem with this code?

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
Helen
  • 87,344
  • 17
  • 243
  • 314
Amit Kumar
  • 1,367
  • 3
  • 13
  • 24
  • Could you show us what you have already? – redhotspike Dec 05 '12 at 12:54
  • possible duplicate of [Can Windows' built-in ZIP compression be scripted?](http://stackoverflow.com/questions/30211/can-windows-built-in-zip-compression-be-scripted) – Shiplu Mokaddim Dec 05 '12 at 12:59
  • 1
    I think that `CopyHere()` is asynchronous and (if this is your full code listing) your script process ends before any files are copied. –  Dec 06 '12 at 09:10
  • @shiplu.mokadd.im: This question is not a duplicate; it asks why the specific code does not work. – Helen Dec 06 '12 at 14:50
  • Could you please simplify your code, so that it doesn't contain anything (variables, statements etc) not related to the question? – Helen Dec 06 '12 at 14:57

1 Answers1

2

This shows waiting 500 ms after creating a new ZIP file before attempting to copy data into it. Have you tried using 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

(Untested)

Sarah Kemp
  • 2,670
  • 3
  • 21
  • 29
  • Yeah I wrote this. The filesystem used to take a second or so to write the file, so I put a pause in there. I think modern SSDs might not need this pause anymore. – Nathan Rice Apr 26 '21 at 00:13