Handy vbscript functions for dealing with zip files and folders.
When it comes to task automation in the windows environment, vbscript is a very good scripting language to use. This post documents some useful functions that I had included in my scripts to deal with zip files and create folders .
Create folders recursively
Although there is a CreateFolder function provided by the FileSystemObject, there are times when we need to create folders within folders recursively. The CreateFolder function provided by the FileSystemObject does not create parent folders of the folder that you want to create. For example, if you supply "a\b\c" as a parameter to the CreateFolder function , vbscript will throw an error when folder a or folder b does not exist. The following vbscript function will create parent folders if they do not already exist.
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Sub CreateFolder(folderUrl)
folderUrl = fso.GetAbsolutePathName(folderUrl)
If (Not fso.folderExists(fso.GetParentFolderName(folderUrl))) then
' Call CreateFolder recursively to create the parent folder
CreateFolder(fso.GetParentFolderName(folderUrl))
End If
' Create the current folder if the parent exists
fso.CreateFolder(folderUrl)
End Sub
Sample usage:
CreateFolder("a\b\c\d\e\f")
Delete folder
When you have automated tasks creating folders, you will want to delete them when you no longer need them. The following vbscript function will do just that.
Sub DeleteFolder(folderUrl)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
folderUrl = fso.GetAbsolutePathName(folderUrl)
If (fso.FolderExists(folderUrl)) Then
fso.DeleteFolder(folderUrl)
End If
End Sub
Sample usage:
DeleteFolder("a")
Create a zip file
Most of the time, results are generated by the automated tasks and we want to be able to gather them at a single location for analysis. By creating a zip file of the results, we can prevent code change at the consumer task which will analyse the results.
I did some modification to the following vbscript functions that I got from a forum posting by Cheeso at stackoverflow.com.
Sub NewZip(pathToZipFile)
'WScript.Echo "Newing up a zip file (" & pathToZipFile & ") "
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim file
Set file = fso.CreateTextFile(pathToZipFile)
file.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
file.Close
Set fso = Nothing
Set file = Nothing
WScript.Sleep 500
End Sub
Sub CreateZip(pathToZipFile, dirToZip)
'WScript.Echo "Creating zip (" & pathToZipFile & ") from (" & dirToZip & ")"
Dim fso
Set fso= Wscript.CreateObject("Scripting.FileSystemObject")
pathToZipFile = fso.GetAbsolutePathName(pathToZipFile)
dirToZip = fso.GetAbsolutePathName(dirToZip)
If fso.FileExists(pathToZipFile) Then
'WScript.Echo "That zip file already exists - deleting it."
fso.DeleteFile pathToZipFile
End If
If Not fso.FolderExists(dirToZip) Then
'WScript.Echo "The directory to zip does not exist."
Exit Sub
End If
NewZip pathToZipFile
dim sa
set sa = CreateObject("Shell.Application")
Dim zip
Set zip = sa.NameSpace(pathToZipFile)
'WScript.Echo "opening dir (" & dirToZip & ")"
Dim d
Set d = sa.NameSpace(dirToZip)
' Look at http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx
' for more information about the CopyHere function.
zip.CopyHere d.items, 4
Do Until d.Items.Count <= zip.Items.Count
Wscript.Sleep(200)
Loop
End Sub
Sample usage, assuming that the results folder had been created:
'Zip up the files in the results folder into a file named as results.zip CreateZip "results.zip", "results"
Extract files from a zip file
The reason for a function to extract files from a zip file is coherent with the reason to create a zip file. To extract files from a zip file, the following vbscript function reverse the process of creating one.
Sub ExtractFilesFromZip(pathToZipFile, dirToExtractFiles)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
pathToZipFile = fso.GetAbsolutePathName(pathToZipFile)
dirToExtractFiles = fso.GetAbsolutePathName(dirToExtractFiles)
If (Not fso.FileExists(pathToZipFile)) Then
WScript.Echo "Zip file does not exist: " & pathToZipFile
Exit Sub
End If
If Not fso.FolderExists(dirToExtractFiles) Then
WScript.Echo "Directory does not exist: " & dirToExtractFiles
Exit Sub
End If
dim sa
set sa = CreateObject("Shell.Application")
Dim zip
Set zip = sa.NameSpace(pathToZipFile)
Dim d
Set d = sa.NameSpace(dirToExtractFiles)
' Look at http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx
' for more information about the CopyHere function.
d.CopyHere zip.items, 4
Do Until zip.Items.Count <= d.Items.Count
Wscript.Sleep(200)
Loop
End Sub
Sample usage, assuming results.zip is a valid zip file:
'Extract files from results.zip to the current directory ExtractFilesFromZip "results.zip", "."
6 Comments
Thanks
You are welcomed, Ousmane! 🙂
Congrats. It works !
Thanks Sébastien!
Hi,
The argument to CopyHere “intOptions”, which controls flags, doesn’t seem to be fully working in Win7: when I set intOptions to 4096 (so it doesn’t create subdirectories from the extracted zip), it still creates recursive folders.
As a result, I can’t navigate to my extracted files, since they exist in a subfolder I don’t know the name or location to.
Any ideas on either how to properly use intOptions with its value set to 4096, or how to find out the subfolders created so I can find the extracted files?
Thanks in advance for any help you can offer! 🙂
Regards,
Lizz
Hi Lizz,
According to MSDN’s VBScript reference for Folder.CopyHere, some of the options can be ignored for compressed files. You can iterate the folders recursively with the Folder object.
Assuming that you have unzipped a folder with the name “unzipped-contents” in the current directory, the following is an example script which can recursively look through the contents of the unzipped items when ran in the current directory:
Sub EchoDirContents(dirname) Dim sa Set sa = CreateObject("Shell.Application") Dim fso Set fso= Wscript.CreateObject("Scripting.FileSystemObject") dirAbsPath = fso.GetAbsolutePathName(dirname) Dim folder Set folder = sa.NameSpace(dirAbsPath) For Each item IN folder.Items If item.IsFolder Then EchoDirContents(item.Path) End if WScript.Echo item.Path Next End Sub EchoDirContents "unzipped-contents"