我正在编写一个脚本,该脚本将利用 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 编写的代码进行修改。 http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP