{"id":89,"date":"2010-12-08T22:42:55","date_gmt":"2010-12-08T14:42:55","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=89"},"modified":"2018-09-02T11:32:41","modified_gmt":"2018-09-02T03:32:41","slug":"handy-vbscript-functions-for-dealing-with-zip-files-and-folders","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/handy-vbscript-functions-for-dealing-with-zip-files-and-folders\/","title":{"rendered":"Handy vbscript functions for dealing with zip files and folders."},"content":{"rendered":"<p>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 .<\/p>\n<h3>Create folders recursively<\/h3>\n<p>Although there is a <code>CreateFolder<\/code> function provided by the FileSystemObject, there are times when we need to create folders within folders recursively. The <code>CreateFolder<\/code> 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 <code>CreateFolder<\/code> 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.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDim fso\r\nSet fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)\r\n\r\nSub CreateFolder(folderUrl)\r\n\r\n    folderUrl = fso.GetAbsolutePathName(folderUrl)\r\n    If (Not fso.folderExists(fso.GetParentFolderName(folderUrl))) then\r\n\t' Call CreateFolder recursively to create the parent folder\r\n\tCreateFolder(fso.GetParentFolderName(folderUrl))\r\n    End If\r\n\r\n    ' Create the current folder if the parent exists\r\n    fso.CreateFolder(folderUrl)\r\n\r\nEnd Sub\r\n<\/pre>\n<p><strong>Sample usage:<\/strong><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nCreateFolder(&quot;a\\b\\c\\d\\e\\f&quot;)\r\n<\/pre>\n<h3>Delete folder<\/h3>\n<p>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.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSub DeleteFolder(folderUrl)\r\n    Dim fso\r\n    Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)\r\n    folderUrl = fso.GetAbsolutePathName(folderUrl)\r\n    If (fso.FolderExists(folderUrl)) Then\r\n        fso.DeleteFolder(folderUrl)\r\n    End If\r\nEnd Sub\r\n<\/pre>\n<p>Sample usage:<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDeleteFolder(&quot;a&quot;)\r\n<\/pre>\n<h3>Create a zip file<\/h3>\n<p>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.<\/p>\n<p>I did some modification to the following vbscript functions that I got from a forum posting by <a href=\"http:\/\/stackoverflow.com\/users\/48082\/cheeso\" target=\"_blank\">Cheeso<\/a> at <a href=\"http:\/\/stackoverflow.com\/questions\/2888972\/write-to-file-using-copyhere-without-using-wscript-sleep\" target=\"_blank\">stackoverflow.com<\/a>.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSub NewZip(pathToZipFile)\r\n\r\n   'WScript.Echo &quot;Newing up a zip file (&quot; &amp; pathToZipFile &amp; &quot;) &quot;\r\n\r\n   Dim fso\r\n   Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)\r\n   Dim file\r\n   Set file = fso.CreateTextFile(pathToZipFile)\r\n\r\n   file.Write Chr(80) &amp; Chr(75) &amp; Chr(5) &amp; Chr(6) &amp; String(18, 0)\r\n\r\n   file.Close\r\n   Set fso = Nothing\r\n   Set file = Nothing\r\n\r\n   WScript.Sleep 500\r\n\r\nEnd Sub\r\n\r\nSub CreateZip(pathToZipFile, dirToZip)\r\n\r\n   'WScript.Echo &quot;Creating zip  (&quot; &amp; pathToZipFile &amp; &quot;) from (&quot; &amp; dirToZip &amp; &quot;)&quot;\r\n\r\n   Dim fso\r\n   Set fso= Wscript.CreateObject(&quot;Scripting.FileSystemObject&quot;)\r\n\r\n   pathToZipFile = fso.GetAbsolutePathName(pathToZipFile)\r\n   dirToZip = fso.GetAbsolutePathName(dirToZip)\r\n\r\n   If fso.FileExists(pathToZipFile) Then\r\n       'WScript.Echo &quot;That zip file already exists - deleting it.&quot;\r\n       fso.DeleteFile pathToZipFile\r\n   End If\r\n\r\n   If Not fso.FolderExists(dirToZip) Then\r\n       'WScript.Echo &quot;The directory to zip does not exist.&quot;\r\n       Exit Sub\r\n   End If\r\n\r\n   NewZip pathToZipFile\r\n\r\n   dim sa\r\n   set sa = CreateObject(&quot;Shell.Application&quot;)\r\n\r\n   Dim zip\r\n   Set zip = sa.NameSpace(pathToZipFile)\r\n\r\n   'WScript.Echo &quot;opening dir  (&quot; &amp; dirToZip &amp; &quot;)&quot;\r\n\r\n   Dim d\r\n   Set d = sa.NameSpace(dirToZip)\r\n\r\n   ' Look at http:\/\/msdn.microsoft.com\/en-us\/library\/bb787866(VS.85).aspx\r\n   ' for more information about the CopyHere function.\r\n   zip.CopyHere d.items, 4\r\n\r\n   Do Until d.Items.Count &lt;= zip.Items.Count\r\n       Wscript.Sleep(200)\r\n   Loop\r\n\r\nEnd Sub\r\n<\/pre>\n<p><strong>Sample usage, assuming that the results folder had been created:<\/strong><\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Zip up the files in the results folder into a file named as results.zip\r\nCreateZip &quot;results.zip&quot;, &quot;results&quot;\r\n<\/pre>\n<h3>Extract files from a zip file<\/h3>\n<p>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.<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSub ExtractFilesFromZip(pathToZipFile, dirToExtractFiles)\r\n\r\n    Dim fso\r\n    Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)\r\n\r\n    pathToZipFile = fso.GetAbsolutePathName(pathToZipFile)\r\n    dirToExtractFiles = fso.GetAbsolutePathName(dirToExtractFiles)\r\n\r\n    If (Not fso.FileExists(pathToZipFile)) Then\r\n        WScript.Echo &quot;Zip file does not exist: &quot; &amp; pathToZipFile\r\n        Exit Sub\r\n    End If\r\n\r\n    If Not fso.FolderExists(dirToExtractFiles) Then\r\n        WScript.Echo &quot;Directory does not exist: &quot; &amp; dirToExtractFiles\r\n        Exit Sub\r\n    End If\r\n\r\n    dim sa\r\n    set sa = CreateObject(&quot;Shell.Application&quot;)\r\n\r\n    Dim zip\r\n    Set zip = sa.NameSpace(pathToZipFile)\r\n\r\n    Dim d\r\n    Set d = sa.NameSpace(dirToExtractFiles)\r\n\r\n    ' Look at http:\/\/msdn.microsoft.com\/en-us\/library\/bb787866(VS.85).aspx\r\n    ' for more information about the CopyHere function.\r\n    d.CopyHere zip.items, 4\r\n\r\n    Do Until zip.Items.Count &lt;= d.Items.Count\r\n        Wscript.Sleep(200)\r\n    Loop\r\n\r\nEnd Sub\r\n<\/pre>\n<p>Sample usage, assuming results.zip is a valid zip file:<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n'Extract files from results.zip to the current directory\r\nExtractFilesFromZip &quot;results.zip&quot;, &quot;.&quot;\r\n<\/pre>\n<h3>Related posts<\/h3>\n<ul>\n<li><a title=\"How to compress and decompress files in C#\" href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-compress-and-decompress-files-in-c\/\" target=\"_blank\">How to compress and decompress files in C#<\/a><\/li>\n<li><a title=\"C# facilities for dealing with folders\" href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/c-facilities-for-dealing-with-folders\/\" target=\"_blank\">C# facilities for dealing with folders<\/a><\/li>\n<li><a title=\"How to interact with applications via command line in C#\" href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-interact-with-applications-via-command-line-in-c\/\" target=\"_blank\">How to interact with applications via command line in C#<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-1r\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-1r&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-1r&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F89&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>\nWhen 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 creating folders.<\/p>\n","protected":false},"author":1,"featured_media":1174,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[11],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/VBScript-logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-1r","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/89"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1174"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}