#!/usr/bin/perl -w
#
# Demonstration of going it alone without File::Find
use strict;
use Cwd;
die "Syntax: $0
" unless @ARGV == 1 && -d $ARGV[0];
find(0, $ARGV[0]);
exit;
sub find
{
my ($level, $dir) = @_;
my ($back) = cwd()
or die "Failed to read current working directory";
chdir($dir) or die "Couldn't cd to $dir";
opendir(DIR, ".") or die "Failed to opendir()";
my @files = grep { $_ ne '.' && $_ ne '..' } readdir(DIR);
closedir(DIR) or die "Failed to closedir()";
for my $file (@files)
{
# If you want a symlink safe version write this
# lstat($file);
# if (-d _)
if (-d $file)
{
# Do something with a directory
print ' ' x $level, "Dir: '$file'\n";
# ...then recurse if you want to
find($level+1, $file);
}
else
{
# Do something with a file (don't recurse)
print ' ' x $level, "File: '$file'\n";
}
}
chdir $back
or die "Failed to chdir $back";
}