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

19,246

Solution 1

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

Solution 2

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
Share:
19,246

Related videos on Youtube

Michael Mankus
Author by

Michael Mankus

I've been programming since I was 16 years old. The majority of my experience is in writing non-commercial software for both the private and government sectors. My specialty is writing testbed applications and data collection / playback applications. Familiar languages include C#, C++, C, and Java.

Updated on September 18, 2022

Comments

  • Michael Mankus
    Michael Mankus over 1 year

    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.

    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 almost 11 years
      We're not here as a script writing service. ;) So, what have you got so far, and where are you getting stuck?
    • Karan
      Karan almost 11 years
      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.
  • Michael Mankus
    Michael Mankus almost 11 years
    I get an error on the last line: "Cannot use parentheses when calling a Sub". Any thoughts on what to fix?
  • STTR
    STTR almost 11 years
    @MichaelMankus Sorry, fixed.
  • CreativiTimothy
    CreativiTimothy almost 6 years
    The batch just closes after calling zipjs.bat Do you know how to fix this?
  • Bhavini
    Bhavini almost 6 years
    @CreativiTimothy Have you added the call at the beginning as in the code snippet?
  • CreativiTimothy
    CreativiTimothy almost 6 years
    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
  • Bhavini
    Bhavini almost 6 years
    @CreativiTimothy - can you paste your whole code somewhere (pastebin,github) so I can check it?
  • CreativiTimothy
    CreativiTimothy almost 6 years
  • CreativiTimothy
    CreativiTimothy almost 6 years
    I 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)