命令行参数-必需的对象:'objshell.NameSpace(...)' [英] Command line arguments - object required: 'objshell.NameSpace(...)'

查看:266
本文介绍了命令行参数-必需的对象:'objshell.NameSpace(...)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个脚本,该脚本将利用Windows的内置功能来解压缩提供的.zip文件.我对vbscript还是很陌生,因此有些语法让我有些难受.我正在使用一些现有代码,并试图对其进行修改,以使该文件名使用命令行选项. 如果我使用命令行传递文件名,则会收到错误消息:

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:

所需对象:'objshell.NameSpace(...)'

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?

这是我的代码:

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)

sSourceFile = WScript.Arguments.Item(0)

这是我尝试对Rob van der Woude编写的代码进行更改. http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

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

推荐答案

尝试

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

代替

sSourceFile = WScript.Arguments.Item(0)

这篇关于命令行参数-必需的对象:'objshell.NameSpace(...)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆