我正在开发一个脚本,该脚本将利用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
答案 0 :(得分:12)
尝试
Set fso = CreateObject("Scripting.FileSystemObject")
sSourceFile = fso.GetAbsolutePathName(WScript.Arguments.Item(0))
而不是
sSourceFile = WScript.Arguments.Item(0)