How to Find Compressed Folders with a Perl Script

How to find compressed folders in on a Windows server using a Perl script.  I've been asked a number of times to find out what folders are compressed on a Windows server, for various reasons.  Once, Microsoft put out a bad patch that hosed up compressed files on Windows 2000 servers.  Another time, it was a DFS replication bug that didn't like compressed files.  So, inevitably I suppose, I eventually reach the conclusion.  I gotta Perl this out.

Perl has some basic file and folder functions, like opendir and readdir, that allow you to get a directory listing.  ActiveState Perl includes a Perl module called Win32::File, that allows you to retrieve attributes of a file or folder (including the compressed attribute).  So the script uses readdir to get the folders at the root of the search, checks to compressed attribute and then recursively reads each subdirectory.  If it finds a compressed folder, it writes the path to the log, and doesn't bother digging through that folder's subdirectories. 

The script also demonstrates how fast Perl is.  You'd be hard pressed to find anything that can do this faster.

use Win32::File;
print "Searching for compressed files in $mydirname/...\n";
open OUT,">compressionLog.txt";
sub processDir{
 my $bailOut=0;
 my ($dirname)=@_;
  $test=Win32::File::GetAttributes("$dirname", $attrs);
  if($attrs & COMPRESSED){
   print "*** $dirname is compressed\n";
   print OUT "$dirname is compressed\n";
  if($bailOut == 0){
   my @subdirs=map ($_=$dirname."/".$_,(grep { !/\./ && !/\.\./ && -d "$dirname/$_" } readdir(DIR)));
   foreach $subdir (@subdirs){
    if(index(lc($subdir),"dfsrprivate") == -1){
close OUT;


Post a Comment

Related Posts Plugin for WordPress, Blogger...