Remove_Empty_Folders.pl

#!/usr/bin/perl -w
# Remove_Empty_Folders.pl
# This script takes a folder as input, recurses through the folder structure, and deletes any empty folders under the original folder.
# The script will NOT delete ANY files, it simply removes folders if they are empty.
# Version 1.0
use strict;
use Cwd;
use File::Glob qw(bsd_glob);
if (scalar @ARGV != 1)
{
    print "USAGE: $0 <folder_to_cleanup>\n";
    print "       <folder_to_cleanup> is the folder containing (suspected) empty subfolders\n";
    print "inputs=\t@ARGV\n";
    exit;
}
my $source_dir = $ARGV[0];
if (!-d $source_dir)
{
    print "Source Dir - $source_dir does not exist - cannot continue.\n";
    exit;
}
######## SUB for removing folders ##############
sub removeEmptyFolders
{
    my $source = $_[0];
    chdir($source) or die("Cannot access folder $source");
    my @directories;
    my @all_files = (bsd_glob("*"),bsd_glob(".*"));
    # Update the current Date and time
    print "*** CLEANING FOLDERS ***\n";
    my $counter = 0;
    foreach (@all_files)
    {
        # check for the dodgy . and .. in the listed path (i.e. current dir and parent dir), as we don't want to recurse into these.
        # ... it's really not very fun when you start running this program outside of the given source path!
        if (($_ eq '.') || ($_ eq '..'))
        {
            #print "$_ matched . or .. and was discarded\n";
        }
        else   
        {
            $counter++;
           
            # Check if the file is a directory or not. Directories don't get MD5'd, but they get added to the directories array for
            # recursive processing to find files.
            if (-d $_)
            {
                #print "Adding new dir - $source\\$_\n";
                push @directories, "$source\\$_";
            }     
        }
    }
   
    if ($counter == 0)
    {
        # this means that no subfolders or files were found, so we should be able to delete the folder
        print "==> CLEANUP - Delete $source\n";   
        return 1;
    }
    else
    {
        foreach (@directories) # recursively call itself to process any directories which have been found
        {
            my $return_value = &removeEmptyFolders($_);
            chdir "$source" or die "cannot chdir to $source: $!";
            if ($return_value == 1)
            {
                # If it returns 1 then delete the folder
                print "==> CLEANUP - Deleting $_\n";
                rmdir ("$_") or print "$!\n";
                print "==> CLEANUP - Deleting $_\n";   
            }
        }
           
        my @second_pass = (bsd_glob("*"),bsd_glob(".*"));
        my $secondcounter = 0;
        foreach (@second_pass)
        {
            if (($_ eq '.') || ($_ eq '..'))
            {
                    #print "$_ matched . or .. and was discarded\n";
            }
            else
            {
                    $secondcounter++;
            }
        }
       
        if ($secondcounter == 0)
        {
            # this means that no subfolders or files were found, so we should be able to delete the folder
            print "==> CLEANUP (Pass 2) - Delete $source\n";
            return 1;
        }
        else
        {
            return 0;
        }
    }
}
### DRIVER FUNCTION ###
my $return_value = &removeEmptyFolders($source_dir);

Post a Comment

Previous Post Next Post