How Can I Create a Compressed Folder and Add Files to It?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a compressed folder and add files to it?

— SL

SpacerHey, Scripting Guy! AnswerTechNet Script Center

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 when the Scripting Spouse says, “Do these jeans make me look fat?” To be honest, we’re not always sure what 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’t believe us? Then try answering this question by saying something on the lines of “Those jeans? Oh, yeah, those jeans make you look fat.”)

Note. At the risk of sounding sexist, if a Scripting Husband should ask a question like that then it’s perfectly fine to say that the jeans make him look fat. (Editor’s Note: Uh, no, that hasn’t been the experience of the Scripting Editor.) After all, most husbands will simply shrug and say, “OK. I guess I might as well have another doughnut then.” (Well, okay, that part is true.)

Of course, that’s also what most husbands would say if you told them that the jeans didn’t make them look fat. Or if you told them the lawn needed mowing or that the house was on fire.

Besides, fat looks good, at least on men.

Or so the male Scripting Guys keep telling themselves. (The female Scripting Guy wouldn’t dream of destroying their illusions.)

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’re talking about using the folder/file compression technology built into the NTFS file system that’s great: in that case we have an answer for you. However, if you’re talking about creating a “zipped” folder (a technology introduced in Windows XP), well, that’s a different story: unfortunately, you can’t create zipped folders programmatically. (Although it might be worth your while to check out this unofficial and unsupported script in the Community-Submitted Scripts Center.) We can compress folders for you (equivalent to right-clicking a folder, clicking Properties, clicking Advanced, and then selecting Compress contents to save disk space). However, we can’t zip folders for you.

In other words, if you were hoping to get an answer to a different question, well, unfortunately we don’t have an answer for you. At least we don’t have an answer other than, “You can’t.”

But as long as it’s compression you want then it’s compression you’ll get. For example, here’s a script that compresses the folder C:\Scripts and all its contents (files and subfolders alike):

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFolders = objWMIService.ExecQuery _ (“Select * From Win32_Directory Where Name = ‘C:\\Scripts'”)

For Each objFolder in colFolders errResults = objFolder.Compress Next

As you can see, there isn’t 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:

Set colFolders = objWMIService.ExecQuery _
    (“Select * From Win32_Directory Where Name = ‘C:\\Scripts'”)

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 Name in our Where clause. Why? One reason and one reason only: in WMI the Name property is equivalent to the file path. (If that’s the case then shouldn’t they have called this property Path rather than Name? Probably, but there’s not much we can do about that at the moment.)

Second, notice how we specify the folder Name (path): C:\\Scripts. The extra \ is important here; in fact, without it the script will fail. That’s because the \ is a reserved character in WMI; any time that character is used in a Where clause it must be “escaped” by prefacing it with another \.

Yes, we agree. But that’s yet another thing we can’t do much about at the moment.

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 Compress method to compress the folder and everything in it. All of that requires just three little lines of code:

For Each objFolder in colFolders
    errResults = objFolder.Compress

Just like that, the folder – and everything in it – will be compressed.

Of course, yours was actually a two-part question, wasn’t 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’t have to do anything special to add files to this compressed folder; after all, compressed or not, it’s 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:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFiles = objWMIService.ExecQuery(“Select * from CIM_DataFile Where Name = ‘D:\\Test.ppt'”)

For Each objFile in colFiles strCopy = “C:\Scripts\” & objFile.FileName & “.” & objFile.Extension objFile.Copy(strCopy) Next

We won’t take the time today to explain how this script works; for more information, see the Microsoft Windows 2000 Scripting Guide. For now we’ll 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.

And speaking of sizes, we now have a question for you: does today’s column make us look fat? Go ahead, take as much time as you need to answer; we’ll just have a doughnut or two while we wait.


Discussion is closed.

Feedback usabilla icon