Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line arguments - object required: 'objshell.NameSpace(...)'

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

like image 869
gritts Avatar asked Oct 03 '12 17:10

gritts


1 Answers

Try

Set fso = CreateObject("Scripting.FileSystemObject")
sSourceFile = fso.GetAbsolutePathName(WScript.Arguments.Item(0))

instead of

sSourceFile = WScript.Arguments.Item(0)
like image 179
Ansgar Wiechers Avatar answered Oct 06 '22 00:10

Ansgar Wiechers