1.

Solve : VBScript to Unzip Issues?

Answer»

I'm not very proficient in VBS, infact, I took this script from another thread on this FORUM, I'm just trying to implement it to my needs.

Set WshShell = CreateObject("Wscript.Shell")
file = WshShell.ExpandEnvironmentStrings("%file%")
direc = WshShell.ExpandEnvironmentStrings("%cd%")

strZipFile = "" & direc & "" & file & ".ZIP"
outFolder = "" & direc & "\" & file & ""

Set objShell = CreateObject( "Shell.Application" )
Set objSource = objShell.NameSpace(strZipFile).Items()
Set objTarget = objShell.NameSpace(outFolder)
intOptions = 256
objTarget.CopyHere objSource, intOptions


%file% expands to input from the batch file the vbscript will be run from. %cd% expands to %cd% normally. I want the script to take a zip file (that is located in the current directory) and place the extracted files in a folder that has the same name as the zip file (minus the extension).

This is the message I get when I try and run the batch file (the contents of which I will show after):
T:\DOCUMENTS and Settings\USERNAME\My Documents\ Stuff\vb.vbs(9, 1) Microsoft VBScript runtime error: Object required: 'objShell.NameSpace(...)'

Here is the contents of the batch file:
@echo off
set file=%1
set file=%file:.zip=%
cscript //nologo vb.vbs
pause

Can someone tell me how to fix it?Why does the batch file remove the .zip extension, and the vbs script add it back again?
Quote from: Salmon Trout on November 26, 2009, 03:48:26 PM

Why does the batch file remove the .zip extension, and the vbs script add it back again?

So if I add .zip it won't look for .zip.zip files, and if I don't then it will automatically add .zip to the END.

But anyway, can you help?The problem is that the VBScript cannot see the environment variables from the batch file. An alternative is to pass the variables on the CScript command line where the VBScript can see them.

Code: [Select]If WScript.Arguments.Unnamed.Count <> 2 Then
WScript.Echo "Invalid arguments...Job Terminated"
WScript.Quit
End If

strZipFile = WScript.Arguments.Unnamed(1) & "\" & WScript.Arguments.Unnamed(0) & ".zip"
outFolder = WScript.Arguments.Unnamed(1)

Set objShell = CreateObject( "Shell.Application" )
Set objSource = objShell.NameSpace(strZipFile).Items()
Set objTarget = objShell.NameSpace(outFolder)
intOptions = 256
objTarget.CopyHere objSource, intOptions

Code: [Select]@echo off
set file=%1
set file=%file:.zip=%
cscript //nologo vb.vbs "%file%" "%cd%"
pause

Run your batch file like you always do.

Good luck.

When I post on the forum, I very rarely put in error checking routines. FEEL free to add your own.Thanks, but can you tell me how to add error-checking routines? I added an if exist test to the batch file, to make sure the .zip exists.Quote
I added an if exist test to the batch file, to make sure the .zip exists.

That's all you really need. Not sure if the CopyHere method will create a new folder if it doesn't exist, but you can test that yourself.



Discussion

No Comment Found