3

I know very little about bash or vbs. I am trying to make a script that will automatically unzip a zip called 'dungeon.zip', which contains a little game I programmed. I want to unzip it to a folder called dungeon in the same directory that the zip file was in. I used the code from this answer, and replaced the files with my files:

strZipFile  = "dungeon.zip"
strUnzipped = "dungeon\"

Sub UnZip(ExtractTo,ZipFile)

Set fso = CreateObject("Scripting.FileSystemObject") 
    If NOT fso.FolderExists(ExtractTo) Then 
       fso.CreateFolder(ExtractTo) 
End If 

Set objShell = CreateObject("Shell.Application") 
Set FilesInZip=objShell.NameSpace(ZipFile).items 

ObjShell.NameSpace(ExtractTo).CopyHere(FilesInZip) 
Set fso = Nothing 
Set objShell = Nothing 
End Sub

set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("MyDocuments")

strZipPath   = strDesktop & strZipFile
strUnzipPath = strDesktop & strUnzipped

UnZip strUnzipPath , strZipPath

As in his answer, I run the .vbs from a cmd file:

cscript UnzipZip.vbs

Here is the error:

C:\Users\Brett\Downloads\UnzipZip.vbs(12, 1) Microsoft VBScript runtime error: Object required: 'objShell.NameSpace(...)'

Any idea on how to fix this?

Community
  • 1
  • 1
Bretsky
  • 423
  • 8
  • 23

4 Answers4

5

WshShell.SpecialFolders("MyDocuments") returns the path without a trailing backslash. You're then appending your filename to it.

You'll need to add a backslash.

strZipPath   = strDesktop & "\" & strZipFile
strUnzipPath = strDesktop & "\" & strUnzipped

Edit to add a tip:

Use the BuildPath() function (it's part of FileSystemObject) to never have to worry about trailing backslashes again.

strZipPath   = fso.BuildPath(strDesktop, strZipFile)
strUnzipPath = fso.BuildPath(strDesktop, strUnzipped)
Bond
  • 16,071
  • 6
  • 30
  • 53
  • I just ran your script, after changing the lines above, and it works fine (using a test `zip` file that I had). Are you sure your `dungeon.zip` file is located in your `My Documents` folder? Are you sure it's a legitimate `zip` file? – Bond Jul 05 '15 at 17:29
1

Your ZipFile in

Set FilesInZip=objShell.NameSpace(ZipFile).items 

is empty ('undefined'). Did you mean strZipFile?

You should use Option Explicit to avoid such blunders.

Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • His code is correct. `ZipFile` is the argument name for the `UnZip` sub. Just hard to tell due to lack of indentation and code above and below the sub.. – Bond Jul 04 '15 at 17:15
1

Set

   strZipFile  = "dungeon.zip\" 

and

   Set FilesInZip=objShell.NameSpace(strZipFile).items

in your code.

arvind123
  • 71
  • 2
1

Just to elaborate the cause of the error. I also encountered the same scenario and it is because the zip file location is not valid or does not exists. Try to put the exact path of the zip file and it will work.

d.i.joe
  • 606
  • 9
  • 22
  • Thanks for the explanation I couldn't figure out why i was getting this error, I put the path in explorer and came to realize i was missing a portion of it! #KUDOS – demo7up Oct 23 '19 at 16:33