|
Answer» Hi,
I wrote the this script to unzip a zip file using windows scripting.
============================================================================== Dim folderPath01 folderPath01 = "C:\AV\" Call UnzipFolder(folderPath01)
FUNCTION UnzipFolder(FoldPath)
Set objShell = CreateObject( "Shell.Application" ) Set objSource = objShell.NameSpace(zipfile).Items() Set objTarget = objShell.NameSpace(FoldPath) intOptions = 256 objTarget.CopyHere objSource, intOptions End Function ==============================================================================
When I run this script I get the following errors:
Object required: objShell.NameSpace(...)
I am running this on Windows 7 - 64 bit. PLEASE let me know what I am doing wrong.
Thanks, ShiyamYou have not specified a value for zipfile
You will need to add:
zipfile = c:\PATH\myfile.zip
to your script.As Oldun has noticed, you have not given a value (needs to be full path and name of zip file) to the variable zipfile in this line therefore you got the error message described.
Set objSource = objShell.NameSpace(zipfile).Items()
Because you have made the unzipping code into a function, you need to pass the path and name of the zip file as a parameter to the function. (Did you really "write" that VBscript?) The following script will show what I mean
I have run this on Windows 7 Professional 64 bit and also XP Professional 32 bit.
Dim folderPath01 folderPath01 = "C:\AV\" Zipfile="C:\Zipfiles\Test.zip" Call UnzipFolder(folderPath01, Zipfile)
Function UnzipFolder(FoldPath, ZipFileName) Set objShell = CreateObject( "Shell.Application" ) Set objSource = objShell.NameSpace(ZipFileName).Items() Set objTarget = objShell.NameSpace(FoldPath) intOptions = 256 objTarget.CopyHere objSource, intOptions End Function
In addition, this method will NOT work if Compressed Folders is disabled. In that situation, you will get this message:
Path\to\script.vbs(line number, char number) (null): Unspecified error
It is simple to Google how to enable/disable Compressed Folders.
Note: As with any file copy operation using CopyHere, the second parameter for CopyHere specifies how the copy from "compressed folder" (zip file) will be carried out:
In your script this is the value (256) you have given to intOptions.
4 Do not display a progress dialog box. 8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists. 16 Respond with "Yes to All" for any dialog box that is displayed. 64 Preserve undo information, if possible. 128 Perform the operation on files only if a wildcard file name (*.*) is specified. 256 Display a progress dialog box but do not show the file names. 512 Do not confirm the creation of a new DIRECTORY if the operation requires one to be created. 1024 Do not display a user interface if an error occurs. 2048 VBScript version 4.71. Do not copy the security attributes of the file. 4096 Only operate in the local directory. Do not operate recursively into subdirectories. 8192 VBScript version 5.0. Do not copy connected files as a GROUP. Only copy the specified files.
Wowww. Thanks Oldun and Salmon for your time.
Salmon,
Thanks a lot for your detail. I am new to windows host scripting. I am learning it from Microsoft Windows Scripting book from MS press. It is written for Windows 2003/XP. So when ever i get an error (specially the object requireed error), I tend to think its because its not available on Windows 7.
Again. I appreciate you explaining intoptions variable, as that was going to be my next question. I have no idea how you saw that coming. I had "zipfile" variable defined on this script even though I forgot to post it, but I had two issues.
1. I did not pass that to the function as a parameter. 2. I did not have the full path "zipfile" on variable.
I have one more question. If I have the .vbs file and the .zip file on the same folder, why doesn't the relative path (i.e. just the filename (file.zip) does not work? Is there any way to acheive that?
Thanks again for the time.
Regards, ShiyamQuote from: shiyam198 on February 06, 2011, 01:11:06 PM Again. I appreciate you explaining intoptions variable, as that was going to be my next question. I have no idea how you saw that coming.
Oh, I just had a feeling...
also I decided it was a useful thing to mention.
QuoteI have one more question. If I have the .vbs file and the .zip file on the same folder, why doesn't the relative path (i.e. just the filename (file.zip) does not work? Is there any way to acheive that? I imagine it does not work because the file copy routine you are calling probably requires a full path for the source folder (zip file really). If you wish to use a script's own folder path then you can get that from WScript.ScriptFullName. Surely that is in your book?
Option Explicit Dim folderPath01 Dim Zipfile Dim ZipfileFullPath Dim ScriptPath Dim ScriptFullName Dim Objshell Dim objSource Dim objTarget Dim intOptions Dim CopyOption
ScriptFullName = WScript.ScriptFullName ScriptPath = Left (scriptFullName, InStrRev ( scriptFullName, WScript.ScriptName) - 1) folderPath01 = "C:\AV\" Zipfile = "test.zip" ZipfileFullPath = ScriptPath & Zipfile CopyOption = 256 DummyValue = UnzipFolder(folderPath01, ZipFileFullPath, CopyOption)
Function UnzipFolder(FoldPath, ZipFileName, IntOptions) Set objShell = CreateObject( "Shell.Application" ) Set objSource = objShell.NameSpace(ZipFileName).Items() Set objTarget = objShell.NameSpace(FoldPath) objTarget.CopyHere objSource, intOptions End Function
|