Unzip file from source to destination within user directory across multiple versions of windows

2

I have a source zip file which is located in a user's "My Documents" directory. It is guaranteed to always be there. I'm looking for a way to create a batch or script which will let me unzip that file to a destination directory also within the user's directory. If the destination is already there, it should first delete the existing destination folder.

Example process:

srcFile = %user%\My Documents\file.zip
destFolder = %user%\My Documents\Unzipped\
if destFolder exists, delete it
unzip srcFile to destFolder

I'm looking for a solution that will work on Windows XP and Windows 7. If possible, I don't want to use a zip application other than the one built into Windows XP/7.

Michael Mankus

Posted 2013-05-28T18:06:04.150

Reputation: 133

1We're not here as a script writing service. ;) So, what have you got so far, and where are you getting stuck? – Ƭᴇcʜιᴇ007 – 2013-05-28T18:50:25.863

Your last sentence is problematic. Not only does the Compressed Folders function suck, it's not possible to access from the command line AFAIK. You'll probably have to invoke a VBS script from your batch file. If you use a 3rd party command line unzipper what you want to do is really trivial.

– Karan – 2013-05-28T19:10:56.247

Answers

2

DelUnzip.cmd:

RD /S /Q "%USERPROFILE%\My Documents\Unzipped"
cscript UnzipZip.vbs

UnzipZip.vbs:

strZipFile  = "\file.zip"
strUnzipped = "\Unzipped\"

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

STTR

Posted 2013-05-28T18:06:04.150

Reputation: 6 180

I get an error on the last line: "Cannot use parentheses when calling a Sub". Any thoughts on what to fix? – Michael Mankus – 2013-05-29T11:15:37.597

@MichaelMankus Sorry, fixed. – STTR – 2013-05-29T17:11:37.560

1

Here I've tried to summarize ways of how can files and folder be zipped without external tools:

And also tried to create a tool for common usage capable of zipping unzipping and few more features. ZIPJS.BAT - it does not need additional files like .vbs nor creates a temp ones.All in one and called like a normal bat file.

to unzip folder of file you can use this:

// unzip content of a zip to given folder.content of the zip will be preserved (-keep yes).Destination will be overwritten (-force yes)
call zipjs.bat unzip -source C:\myDir\myZip.zip -destination C:\MyDir -keep yes -force yes

npocmaka

Posted 2013-05-28T18:06:04.150

Reputation: 887

The batch just closes after calling zipjs.bat Do you know how to fix this? – CreativiTimothy – 2018-07-24T23:45:57.953

@CreativiTimothy Have you added the call at the beginning as in the code snippet? – npocmaka – 2018-07-25T11:28:49.453

Yeah it's like this: call zipjs.bat unzip -source C:\Users\Creat\Downloads\autoduel.zip -destination C:\Users\Creat\Google Drive\Duel Links\Assembly-CSharp.dll -keep yes -force yes – CreativiTimothy – 2018-07-27T00:52:11.173

@CreativiTimothy - can you paste your whole code somewhere (pastebin,github) so I can check it? – npocmaka – 2018-07-27T06:08:34.960

1https://pastebin.com/qPPN3UNu @npocmaka – CreativiTimothy – 2018-07-31T19:50:44.847

1I just realized it has nothing to do with code itself but that the zip was corrupted because I tried to download using web invoke request on a website that requires authentication (username/password) – CreativiTimothy – 2018-07-31T20:59:39.900