# findEmptyFiles.pl reports all files in a folder with a size of 0 bytes. # Original code written 24-sep-1999 by Frank Brown. require "ctime.pl"; ### global variables: ### $bDebug = 0; $sAppName = "findEmptyFiles"; $sDefRptFile = $sAppName . ".rpt"; $nDirsRead = 0; $nEmptyFiles = 0; $nFilesRead = 0; $sNow = &ctime(time); chop $sNow; $bRecurse = 0; $sRoot = ""; $sTmpDir = $ENV{'TEMP'}; $sRptFile = ""; $sVersion = "1.0"; ### let's go: MAIN: unless ($#ARGV + 1) { die "No directory on command line.\n"; # somebody give me an arg! } &ParseCmdArgs(); if ($sRoot eq "") { die "Root folder missing from command line"; } print "$sAppName $sVersion starting on $sNow...\n"; &ProcessDir($sRoot); $sNow = &ctime(time); print "\n$sAppName $sVersion finished at $sNow"; if ($nEmptyFiles == 0) { print "No empty files found.\n"; goto _noEmpty; } $sMsg = "$sAppName: $nEmptyFiles empty file"; if ($nEmptyFiles > 1) { $sMsg .= "s"; } $sMsg .= " found.\n"; print $sMsg; if ($sRptFile ne "") { &WriteReport(); } _noEmpty: $sFword = "file"; if ($nFilesRead > 1) { $sFword .= "s"; } $sDirWord = "folder"; if ($nDirsRead > 1) { $sDirWord .= "s"; } print "Processed $nFilesRead $sFword in $nDirsRead $sDirWord.\n"; exit; ######## subroutines start here: ######### sub Help { print "\n$sAppName $sVersion lists files with size of 0 bytes.\n\n"; print "usage: $sAppName [/s] [/out=reportFile] dir\n\n"; print "/s\trecurse through subdirectories\n"; print "/out\twrite report file\n"; } sub ParseCmdArgs { while (@ARGV) { $_ = shift @ARGV; if ($bDebug) { print "debug: parsing cmdline arg $_\n"; } if (/^[\/\-]/) { # is arg a switch? $arg = substr($_,1,3); $arg =~ tr/A-Z/a-z/; # lowercase arg if ($arg eq "deb") { $bDebug = 1; } if ($bDebug) { print "dbg: arg=$arg\n"; } if ($arg eq "hel" || substr($arg,0,1) eq '?') { &Help(); exit; } if (substr($arg,0,1) eq 's') { $bRecurse = 1; } if ($arg eq "out") { $i = index($_,"="); $i++; $sRptFile = substr($_,$i); if ($sRptFile eq "") { $sRptFile = $sDefRptFile; } } } else { $sRoot = $_; } } if ($bDebug) { print "dbg: bDebug = $bDebug\n"; print "dbg: sRoot = $sRoot\n"; print "dbg: recurse = $bRecurse\n"; if ($sRptFile ne "") { print "dbg: sRptFile = $sRptFile\n"; } } } sub ProcessDir { local $sDir = $_[0]; if (!length($sDir)) { return; } if ($sDir eq "." || $sDir eq "..") { return; } print "processing dir $sDir...\n"; if (!opendir(FLDR,$sDir)) { print "Can't open folder $sDir\n"; return; } ++$nDirsRead; local @filenames = readdir(FLDR); # get list of files closedir(FLDR); foreach (@filenames) { if ($_ eq "." || $_ eq "..") { next; } $sFile = $sDir; if ($sDir !~ m/\\+$/) { $sFile .= "\\"; } # if no \\ then add one $sFile .= $_; if ( -f $sFile) { &ProcessFile($sFile); } else { if ($bRecurse) { &ProcessDir($sFile); } } } } sub ProcessFile { local $sChkFile = $_[0]; local $nSize; ++$nFilesRead; $nSize = (stat($sChkFile))[7]; if ($nSize == 0) { print " $sChkFile\n"; # prepend a space so it's easier to see on screen push(@files,$sChkFile); ++$nEmptyFiles; } } sub WriteReport { local $sLine = ""; local $sFile = ">" . $sRptFile; if (!open(RPT,$sFile)) { print "Can't open report file $sRptFile: $!"; return; } local $nTotal = @files; @sortedFiles = sort @files; $sNow = &ctime(time); print RPT "$sAppName $sVersion empty file report $sNow\n"; print RPT "Found $nTotal empty files in $sRoot:\n\n"; foreach (@sortedFiles) { print RPT "$_\n"; } print RPT "\n*** end of report ***\n"; close RPT; print "Created report $sRptFile\n"; }