2012-10-03 54 views
6

Estoy trabajando en un script que utilizará las capacidades integradas de Windows para descomprimir un archivo .zip suministrado. Soy bastante nuevo en vbscript, así que parte de la sintaxis me desconcierta un poco. Estoy trabajando con un código existente e intento modificarlo para que tome una opción de línea de comando para el nombre del archivo. Si uso la línea de comandos para pasar el nombre de archivo, recibo el error:Argumentos de línea de comandos - objeto requerido: 'objshell.NameSpace (...)'

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

Si pueblan la misma variable con el texto en el script, el script se ejecuta sin errores. ¿Hay alguna otra pieza que me falta cuando intento usar argumentos de comando?

Aquí está mi código:

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 

La línea de referencia es

sSourceFile = WScript.Arguments.Item(0)

Este es mi intento de hacer una variación en el código escrito por Rob van der Woude. http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

Respuesta

12

Trate

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

en lugar de

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

que funcionó como un campeón. Entonces puedo entender mejor, ¿por qué usar la ruta absoluta del archivo en este caso funciona mejor que lo que estaba usando antes? Supongo que estaba usando una ruta relativa antes? ¿O mi script intentó buscar el archivo en otro directorio sin que yo lo supiera? ¡Gracias por la ayuda! – gritts

+0

Sospecho que antes usaba una ruta de origen relativa. –

Cuestiones relacionadas