2012-10-03 32 views
6

Tôi đang làm việc trên tập lệnh sẽ sử dụng khả năng tích hợp sẵn của Windows để giải nén tệp .zip được cung cấp. Tôi khá mới với vbscript vì vậy một số cú pháp stumps tôi một chút. Tôi đang làm việc với một số mã hiện có và cố gắng sửa đổi nó để nó sẽ có một tùy chọn dòng lệnh cho tên tập tin. Nếu tôi sử dụng dòng lệnh để vượt qua các tên tập tin, tôi nhận được lỗi:Đối số dòng lệnh - đối tượng được yêu cầu: 'objshell.NameSpace (...)'

object required: 'objshell.NameSpace(...)'

Nếu tôi cư biến cùng với văn bản trong kịch bản, kịch bản chạy không bị lỗi. Có một số mảnh khác tôi đang thiếu khi cố gắng sử dụng các đối số lệnh?

Đây là mã của tôi:

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 

Dòng tham chiếu là

sSourceFile = WScript.Arguments.Item(0)

Đây là nỗ lực của tôi để thực hiện một biến thể của mã được viết bởi Rob van der Woude. http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

Trả lời

12

Hãy thử

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

thay vì

sSourceFile = WScript.Arguments.Item(0) 
+0

đó đã làm việc như một nhà vô địch. Vì vậy, tôi có thể hiểu rõ hơn, tại sao việc sử dụng đường dẫn tuyệt đối của tệp trong trường hợp này hoạt động tốt hơn so với những gì tôi đã sử dụng trước đây? Tôi cho rằng tôi đã sử dụng một con đường tương đối trước đây? Hay tập lệnh của tôi có cố gắng tìm tệp trong thư mục khác mà tôi không biết? Cảm ơn bạn đã hỗ trợ! – gritts

+0

Tôi nghi ngờ rằng bạn đang sử dụng đường dẫn nguồn tương đối trước đây. –

Các vấn đề liên quan