I am working on a script that will utilize the built in capabilities of Windows to unzip a supplied .zip file. I am pretty new to vbscript so some of the syntax stumps me a little. I am working with some existing code and trying to modify it so that it will take a command line option for the file name. If I use the command line to pass the file name, I receive the error:
object required: 'objshell.NameSpace(...)'
If I populate the same variable with text within the script, the script runs error free. Is there some other piece I am missing when attempting to use command arguments?
Here is my code:
Option Explicit
Dim sDestinationDirectory,sLogDestination,fso,outLog,sJunk,sSourceFile
sDestinationDirectory = "C:\scripts\vbscriptTemplates\unzip"
sLogDestination = "C:\scripts\vbscriptTemplates\"
Set fso=CreateObject("Scripting.FileSystemObject")
Set outLog = fso.OpenTextFile("unzipRIP.log", 2, True)
If WScript.Arguments.Count = 1 Then
sSourceFile = WScript.Arguments.Item(0) 'Using this line the code will fail.
'sSourceFile = "C:\scripts\vbscriptTemplates\test.zip" 'Using this line the code will run.
outLog.WriteLine ".:|Processing new zip file|:."
outLog.WriteLine "Processing file: " & sSourceFile
Extract sSourceFile,sDestinationDirectory
Else
sJunk = MsgBox("File to be processed could not be found. Please verify.",0,"Unzip - File not found")
outLog.WriteLine "File to be processed could not be found. Please verify."
outLog.Close
Wscript.Quit
End If
Sub Extract( ByVal myZipFile, ByVal myTargetDir )
Dim intOptions, objShell, objSource, objTarget
outLog.WriteLine "Processing file in subroutine: " & myZipFile & " target " & myTargetDir
' Create the required Shell objects
Set objShell = CreateObject( "Shell.Application" )
' Create a reference to the files and folders in the ZIP file
Set objSource = objShell.NameSpace( myZipFile ).Items()
' Create a reference to the target folder
Set objTarget = objShell.NameSpace( myTargetDir )
intOptions = 4
' UnZIP the files
objTarget.CopyHere objSource, intOptions
' Release the objects
Set objSource = Nothing
Set objTarget = Nothing
Set objShell = Nothing
End Sub
The line referenced is
sSourceFile = WScript.Arguments.Item(0)
This is my attempt to make a variation on the code written by Rob van der Woude. http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP
Try
Set fso = CreateObject("Scripting.FileSystemObject")
sSourceFile = fso.GetAbsolutePathName(WScript.Arguments.Item(0))
instead of
sSourceFile = WScript.Arguments.Item(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With