15

I want to unzip a .zip file using VBScript, only it's always a new computer with no external applications on it. Now I know Windows XP and 2003 have an internal .zip folder option, so I guess I can use it via VBScript in order to extract the file.

How do I do it?

I tried:

Set objShell = CreateObject("Shell.Application")

Set SrcFldr = objShell.NameSpace(fileName)
Set DestFldr = objShell.NameSpace(appDir)
DestFldr.CopyHere(SrcFldr) 

Which didn't work. What could be the problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aviv
  • 2,719
  • 7
  • 35
  • 48

1 Answers1

35

Just set ZipFile = The location of the zip file, and ExtractTo = to the location the zip file should be extracted to.

'The location of the zip file.
ZipFile="C:\Test.Zip"
'The folder the contents should be extracted to.
ExtractTo="C:\Test\"

'If the extraction location does not exist create it.
Set fso = CreateObject("Scripting.FileSystemObject")
If NOT fso.FolderExists(ExtractTo) Then
   fso.CreateFolder(ExtractTo)
End If

'Extract the contants of the zip file.
set objShell = CreateObject("Shell.Application")
set FilesInZip=objShell.NameSpace(ZipFile).items
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Tester101
  • 8,042
  • 13
  • 55
  • 78
  • 2
    When writing the same in JScript one needs to take care to escape the backslashes ("\\"). That one gave me a major headache. – Max Leske Jul 21 '15 at 06:41
  • 1
    Getting error as object Required. " set FilesInZip=objShell.NameSpace(ZipFile).items". Can someone help me on this – Arul Sidthan Feb 19 '18 at 08:31
  • 1
    Enter full file path or concatenate "CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) with "file.zip" – raymarch Oct 25 '18 at 01:01
  • @Tester101 Hi, so, plz, if files exist, how can code to do overwrite all of them, without ask for it? – Io-oI Feb 13 '19 at 15:13
  • I just wanna note that the path must be absolute path and the file must have `.zip` extension (in some occasions, a ZIP file, but it might have a different extension - it must be renamed before extraction). – tukusejssirs May 11 '19 at 19:49
  • 1
    Could anyone enhance this code to display an error in case the zip file is corrupt and cannot be opened? In windows 7 this script displays no error in case the zipped file is corrupt. (Asking as I do not know vbscript at all and little bit of novice tinkering didn't help) – Mandeep Singh Jan 10 '20 at 14:18
  • Mandeep, you could add "error handling". vbs allows you to continue if an error happens, and then check to see if an error happened. "on error resume next" then check for error with "If Err <> 0 Then". And if you have a process running the script, be sure to run the script from a bat file that calls the script and then either writes ">" or appends ">>" into a log. You can make a name up. so it would be like "cscript C:\Directory\Myscript.vbs >> C:\Directory\MyLog.txt" and then run the job by running the batch. That will log any wscript.echo and if any messages pop up. – PopeDarren Feb 05 '21 at 20:59