6

提供された.zipファイルを解凍するためにWindowsの組み込み機能を利用するスクリプトに取り組んでいます。私は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 vanderWoudeによって書かれたコードにバリエーションを加えるための私の試みです。 http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

4

1 に答える 1

12

試す

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

それ以外の

sSourceFile = WScript.Arguments.Item(0)
于 2012-10-03T23:27:44.273 に答える