gpt4 book ai didi

object - 命令行参数 - 需要对象 : 'objshell.NameSpace(...)'

转载 作者:行者123 更新时间:2023-12-02 08:38:47 25 4
gpt4 key购买 nike

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

object required: '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 编写的代码进行修改的尝试。 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)

关于object - 命令行参数 - 需要对象 : 'objshell.NameSpace(...)' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com