View Full Version : Perl scripting file handling
Carnal knowledge
10-16-2002, 09:02 AM
Any Perl experts out there?
I've got a a bunch of (diff) files in a directory, and I want to remove the zero length files, which comprise about 95% of them, in a script.
Any ideas on what the easiest way to do this would be?
Thanks.
CurtC
10-16-2002, 09:36 AM
Use the glob() function to return the list of files, then go through each one, using the stat() function to find the file size. If it's zero, delete it with the unlink() function.
friedo
10-16-2002, 11:25 AM
If you're dealing with recursively searching directories, then File::Find (http://search.cpan.org/author/JHI/perl-5.8.0/lib/File/Find.pm) is a very useful module.
Otherwise, I would do something simple like this:
use strict;
my $dir = shift;
opendir DIR $dir or die "Could not open $dir: $!";
my @files = grep !/^\./, readdir DIR;
for(@files) {
unlink $_ if 0 == (stat($_))[7];
}
An excellent site for asking Perl questions is Perl Monks (http://www.perlmonks.org/).
friedo
10-16-2002, 11:30 AM
If you're dealing with recursively searching directories, then File::Find (http://search.cpan.org/author/JHI/perl-5.8.0/lib/File/Find.pm) is a very useful module.
Otherwise, I would do something simple like this:
use strict;
my $dir = shift;
opendir DIR $dir or die "Could not open $dir: $!";
my @files = grep !/^\./, readdir DIR;
for(@files) {
unlink $_ if 0 == (stat($_))[7];
}
An excellent site for asking Perl questions is Perl Monks (http://www.perlmonks.org/).
vBulletin® v3.7.3, Copyright ©2000-2013, Jelsoft Enterprises Ltd.