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

蹲街弑〆低调 提交于 2020-01-13 09:24:08

问题


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


回答1:


Try

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

instead of

sSourceFile = WScript.Arguments.Item(0)


来源:https://stackoverflow.com/questions/12713740/command-line-arguments-object-required-objshell-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!