Code Vault

Code for Unzipping a Zip File

Following is a VBS function that unzips the given file into the given path :



Public Function Extract_Zip(TestZip,ExtractToTest)

'TestZip is the Zip file to be extracted and ExtractToTest is the location where it needs to be extracted
'ensuring that Testzip has .zip extension
TestZip=Replace(TestZip,".zip.zip",".zip")
If instr(1,TestZip,".zip",1)=0 Then
TestZip=TestZip&".zip"
End If

'Deleting and then creating folder where zip file is extracted
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(ExtractToTest) Then
fso.DeleteFolder(ExtractToTest)
End If

If NOT fso.FolderExists(ExtractToTest) Then
fso.CreateFolder(ExtractToTest)
End If

'Extract the contents of the zip file.
set objShell = CreateObject("Shell.Application")
set FilesInZip=objShell.NameSpace(TestZip).items
objShell.NameSpace(ExtractToTest).CopyHere(FilesInZip)
Set fso=nothing
Set objShell=nothing

End Function


Happy Automating !

Harshit Kohli

Advertisements

Categories: Code Vault

Tagged as: , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s