{"id":66963,"date":"2006-07-06T08:09:00","date_gmt":"2006-07-06T08:09:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/06\/how-can-i-create-a-compressed-folder-and-add-files-to-it\/"},"modified":"2006-07-06T08:09:00","modified_gmt":"2006-07-06T08:09:00","slug":"how-can-i-create-a-compressed-folder-and-add-files-to-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-compressed-folder-and-add-files-to-it\/","title":{"rendered":"How Can I Create a Compressed Folder and Add Files to It?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I create a compressed folder and add files to it?<BR><BR>&#8212; SL<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"TechNet Script Center\" border=\"0\" alt=\"TechNet Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, SL. You know, one thing that the Scripting Guys have learned over the years is that when people ask a question they often expect to get an answer to a <I>different<\/I> question. The classic example, of course, is when the Scripting Spouse says, \u201cDo these jeans make me look fat?\u201d To be honest, we\u2019re not always sure <I>what<\/I> question the Scripting Spouse is asking here, but we know one thing: whatever that question is, it has nothing to do with that pair of jeans. (Don\u2019t believe us? Then try answering this question by saying something on the lines of \u201cThose jeans? Oh, yeah, those jeans make you look fat.\u201d)<\/P>\n<TABLE id=\"E6C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. At the risk of sounding sexist, if a Scripting <I>Husband<\/I> should ask a question like that then it\u2019s perfectly fine to say that the jeans make him look fat. <I>(Editor\u2019s Note: Uh, no, that hasn\u2019t been the experience of the Scripting Editor.)<\/I> After all, most husbands will simply shrug and say, \u201cOK. I guess I might as well have another doughnut then.\u201d <I>(Well, okay, that part is true.)<\/I><\/P>\n<P>Of course, that\u2019s also what most husbands would say if you told them that the jeans <I>didn\u2019t<\/I> make them look fat. Or if you told them the lawn needed mowing or that the house was on fire.<\/P>\n<P>Besides, fat looks good, at least on men.<\/P>\n<P>Or so the male Scripting Guys keep telling themselves. <I>(The female Scripting Guy wouldn\u2019t dream of destroying their illusions.)<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So did we have a reason for mentioning all this? Of course we did; after all, SL, in your question you asked about creating a compressed folder. If you\u2019re talking about using the folder\/file compression technology built into the NTFS file system that\u2019s great: in that case we have an answer for you. However, if you\u2019re talking about creating a \u201czipped\u201d folder (a technology introduced in Windows XP), well, that\u2019s a different story: unfortunately, you can\u2019t create zipped folders programmatically. (Although it might be worth your while to check out <A href=\"http:\/\/null\/technet\/scriptcenter\/csc\/scripts\/files\/compressed\/cscfi059.mspx\"><B>this unofficial and unsupported script<\/B><\/A> in the Community-Submitted Scripts Center.) We can compress folders for you (equivalent to right-clicking a folder, clicking <B>Properties<\/B>, clicking <B>Advanced<\/B>, and then selecting <B>Compress contents to save disk space<\/B>). However, we can\u2019t zip folders for you. <\/P>\n<P>In other words, if you were hoping to get an answer to a different question, well, unfortunately we don\u2019t have an answer for you. At least we don\u2019t have an answer other than, \u201cYou can\u2019t.\u201d<\/P>\n<P>But as long as it\u2019s compression you want then it\u2019s compression you\u2019ll get. For example, here\u2019s a script that compresses the folder C:\\Scripts and all its contents (files and subfolders alike):<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Directory Where Name = &#8216;C:\\\\Scripts'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    errResults = objFolder.Compress\nNext\n<\/PRE>\n<P>As you can see, there isn\u2019t much to this. We begin by connecting to the WMI service on the local computer (although, as usual, the script works equally well against remote computers). We then use this line of code to retrieve a collection of all the folders that have the path C:\\Scripts:<\/P><PRE class=\"codeSample\">Set colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Directory Where Name = &#8216;C:\\\\Scripts'&#8221;)\n<\/PRE>\n<P>Two things to note about this query. To begin with, even though we need to specify the full path to the folder, you might note that we use the property <B>Name<\/B> in our Where clause. Why? One reason and one reason only: in WMI the Name property is equivalent to the file path. (If that\u2019s the case then shouldn\u2019t they have called this property Path rather than Name? Probably, but there\u2019s not much we can do about that at the moment.)<\/P>\n<P>Second, notice how we specify the folder Name (path): <B>C:\\\\Scripts<\/B>. The extra \\ is important here; in fact, without it the script will fail. That\u2019s because the \\ is a reserved character in WMI; any time that character is used in a Where clause it must be \u201cescaped\u201d by prefacing it with another \\.<\/P>\n<P>Yes, we agree. But that\u2019s yet another thing we can\u2019t do much about at the moment.<\/P>\n<P>As we noted, this query returns a collection of all the folders with the Name C:\\Scripts (and, seeing as how folder names must be unique, that means our collection will have, at most, a single item). All we have to do then is set up a For Each loop to loop through all the items in the collection. Inside that loop we do just one thing: we call the <B>Compress<\/B> method to compress the folder and everything in it. All of that requires just three little lines of code:<\/P><PRE class=\"codeSample\">For Each objFolder in colFolders\n    errResults = objFolder.Compress\nNext\n<\/PRE>\n<P>Just like that, the folder &#8211; and everything in it &#8211; will be compressed.<\/P>\n<P>Of course, yours was actually a two-part question, wasn\u2019t it? Not only did you want to know how to compress a folder, but you also wanted to know how to copy files into that folder. Turns out that this is even easier. You don\u2019t have to do anything special to add files to this compressed folder; after all, compressed or not, it\u2019s still just a folder in the file system. For example, to copy the file D:\\Test.ppt to your newly-compressed folder you could use a script similar to this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery(&#8220;Select * from CIM_DataFile Where Name = &#8216;D:\\\\Test.ppt'&#8221;)<\/p>\n<p>For Each objFile in colFiles\n   strCopy = &#8220;C:\\Scripts\\&#8221; &amp; objFile.FileName &amp; &#8220;.&#8221; &amp; objFile.Extension\n   objFile.Copy(strCopy)\n Next\n<\/PRE>\n<P>We won\u2019t take the time today to explain how this script works; for more information, see the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_fil_mrse.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>. For now we\u2019ll simply note that not only does the file get copied to C:\\Scripts, but that copy will be compressed as well; in fact, anything copied to or created in C:\\Scripts will automatically be compressed. And this compression is pretty good, too: for example, this particular PowerPoint presentation has a compressed size of 152 KB compared to an uncompressed size of 439 KB. All in all, not too bad.<\/P>\n<P>And speaking of sizes, we now have a question for you: does today\u2019s column make us look fat? Go ahead, take as much time as you need to answer; we\u2019ll just have a doughnut or two while we wait.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a compressed folder and add files to it?&#8212; SL Hey, SL. You know, one thing that the Scripting Guys have learned over the years is that when people ask a question they often expect to get an answer to a different question. The classic example, of course, is [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,3,12,5],"class_list":["post-66963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I create a compressed folder and add files to it?&#8212; SL Hey, SL. You know, one thing that the Scripting Guys have learned over the years is that when people ask a question they often expect to get an answer to a different question. The classic example, of course, is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66963\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}