## Enable compression on a folder tree

Windows compression can save alot of space but can take a very long time if there’s alot of folders and files. In the past I’ve found that when there is alot of data the compression can stop part of the way through. To get around it I’ve written a script to essentially do the same thing.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")

ShowSubFolders (objFSO.GetFolder("C:\Path\to\folder"))

on error  resume next

Sub ShowSubFolders(Folder)
on error resume next
strFunctionName="ShowSubFolders"

For Each Subfolder in Folder.SubFolders
compress_folder(Subfolder.Path)
ShowSubFolders Subfolder
Next
End Sub

function compress_folder(strPath)
on error resume next
strPath=replace(strPath,"\","\\")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where name = '" & strPath & "'")
For Each objFolder in colFolders
wscript.echo strPath
objFolder.compress
Next
end function

The script parses each folder under the folder you specify (replace the c:\Path\to\folder with your own path). ShowSubFolders is called again and again until the tree has been parsed.

The line that does the compression on a folder the files directly within is

objFolder.compress

To use open an administrator command prompt and type

cscript /nologo compress_tree.vbs

Download Script – (Right click -> Save As -> Rename from doc to vbs)