Command Line Arguments - Required Object: 'objshell.NameSpace (...)'

I am working on a script that will use the built-in features of Windows to unpack the supplied .zip file. I am new to vbscript, so some of the syntaxes make me stumble a little. I am working with some existing code and trying to change it so that it uses the command line parameter for the file name. If I use the command line to pass the file name, I get an error message:

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

If I fill the same variable with text inside the script, the script works without errors. Is there any other part that I skip when trying to use the command arguments?

Here is my code:

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 

Linked String

sSourceFile = WScript.Arguments.Item (0)

This is my attempt at modifying the code written by Rob van der Wood. http://www.robvanderwoude.com/vbstech_files_zip.php#CopyHereUNZIP

+6
source share
1 answer

Try

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

instead

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

Source: https://habr.com/ru/post/926871/


All Articles