Object 命令行参数-需要对象:';名称空间(…)';

Object 命令行参数-需要对象:';名称空间(…)';,object,vbscript,namespaces,Object,Vbscript,Namespaces,我正在编写一个脚本,它将利用Windows的内置功能来解压缩提供的.zip文件。我对vbscript非常陌生,所以有些语法让我有点困惑。我正在使用一些现有的代码,并试图修改它,以便它将采用一个命令行选项作为文件名。 如果使用命令行传递文件名,则会收到错误: 所需对象:“objshell.NameSpace(…)” 如果在脚本中用文本填充同一变量,则脚本将无错误运行。在尝试使用命令参数时,我是否还遗漏了其他部分 这是我的密码: Option Explicit Dim sDestinationDi

我正在编写一个脚本,它将利用Windows的内置功能来解压缩提供的.zip文件。我对vbscript非常陌生,所以有些语法让我有点困惑。我正在使用一些现有的代码,并试图修改它,以便它将采用一个命令行选项作为文件名。 如果使用命令行传递文件名,则会收到错误:

所需对象:“objshell.NameSpace(…)”

如果在脚本中用文本填充同一变量,则脚本将无错误运行。在尝试使用命令参数时,我是否还遗漏了其他部分

这是我的密码:

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
引用的行是

sSourceFile=WScript.Arguments.Item(0)

这是我试图对Rob van der Woude编写的代码进行修改的尝试。 试试看

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

sSourceFile = WScript.Arguments.Item(0)

这就像一个冠军。所以我可以更好地理解,为什么在本例中使用文件的绝对路径比我以前使用的更好?我想我以前使用的是相对路径?还是我的脚本试图在我不知道的情况下在另一个目录中查找该文件?谢谢你的帮助!我怀疑您以前使用的是相对源路径。