Help overwriting files with VBScript

May 9, 2009 at 11:07 AM

Hi I have tried to use this script below to overwrite a file.

I get the following error on this line 12

LINE 12 "call zip.ExtractAll("C:\Temp\Zip", ExtractExistingFileAction.OverwriteSilently)

ERROR "Microsoft VBScript runtime error: Object required: 'ExtractExistingFileAction'"

Please let me know if i am missing something.

TIA.

 

WScript.echo("Instantiating a ZipFile object...")
dim zip
set zip = CreateObject("Ionic.Zip.ZipFile")

WScript.echo("Initialize (Read)...")
zip.Initialize("C:\Temp\Zip\ZipFile-created-from-VBScript.zip")

WScript.echo("setting the password for extraction...")
zip.Password = "This is the Password."

WScript.echo("extracting all files...")
call zip.ExtractAll("C:\Temp\Zip", ExtractExistingFileAction.OverwriteSilently)

WScript.echo("Disposing...")
zip.Dispose()

WScript.echo("Done.")

Coordinator
May 9, 2009 at 8:01 PM

Try this code.

WScript.echo("Instantiating a ZipFile object...")
Dim zip 
Set zip = CreateObject("Ionic.Zip.ZipFile")

WScript.echo("Initialize (Read)...")
zip.Initialize("C:\Temp\ZipFile-created-from-VBScript.zip")

WScript.echo("setting the password for extraction...")
zip.Password = "This is the Password."

' set the default action for extracting an existing file
' 0 = throw exceptio
' 1 = overwrite silently
' 2 = don't overwrite (silently)
' 3 = invoke the ExtractProgress event
zip.ExtractExistingFile = 1

WScript.echo("extracting all files...")
Call zip.ExtractAll("extract")

WScript.echo("Disposing...")
zip.Dispose()

WScript.echo("Done.")

some explanation: The ExtractExistingFileAction is a symbol defined in the .NET assembly, but it is not accessible from VBScript. So when you specified it in your script, the VBScript compiler blew up with "Object required." I think that means it does not know that symbol.

That symbol is an enum in .NET, which just means it can take one of a list of values. Instead of using the symbolic names, you can just use the values, which I have documented in the comments of the script above.

But you still cannot call ExtractAll("directory", 1), because of a problem with using overloaded methods from late-bound environments.

Just in case you are not aware, in object-oriented lingo, method overloadeding refers to the practice of defining multiple methods with the same name on a single class; the methods are differentiated by the number and type of parameters they accept. ExtractAll is such an "overloaded method" in the ZipFile class. When you call an overloaded method from VBSCript, the VBSCript compiler can choose the particular method to call by looking at the number of parameters. Unfortunately, there are multiple overloads of ExtractAll that take 2 parameters. This presents a problem for VBScript, which cannot distinguish between overloads based on type alone. So in fact, the way I have delivered the DotNetZip library, you cannot call the ExtractAll method that takes a directory name and an ExtractExistingFileAction param, from VBScript.

The way to do what you want, then, is to set the ExtractExistingFileAction on the ZipFile instance itself, and then call the ExtractAll overload that accepts just one parameter. Since there is only one ExtractAll overload that accepts one parameter, VBScript knows which one to call. It calls the appropriate method, and inside the DotNetZip library, the extraction uses the instance-specific behavior that you had set when extracting an existing file. By default the instance-specific behavior is to throw an exception when extraction would overwrite an existing file. Setting the property to 1 means it will overwrite the existing file silently.

Make sense?