I am trying to write a script that will traverse a file system from a given starting point. I had no problem doing this in java, but converting it to perl wasnt as easy. So far, the script explores all the directies sitting in the relative root dir(whatever starting point i give it)... and prints the files in those directories, but doesnt explore any of their subdirectories. Here is what I have so far....
#!/usr/bin/perl -s
my $start_point=".";
&Traverse($start_point);
sub Traverse{
my ($workdir) = shift;
opendir(DIR, $workdir) or die "Unable to open $workdir:$!\n";
my @dirarray = readdir(DIR) or die "Unable to read $workdir:$!\n";
&PrintFileInfo($workdir);
foreach my $name (@dirarray){
next if $name eq ".";
next if $name eq "..";
if(-d $name){
&Traverse($name);
} else {
&PrintFileInfo($name);
}
}
}
sub PrintFileInfo{
my ($file) = shift;
if(-d $file){
print "DIRECTORY $file\n";
} else {
print "\tFILE $file\n";
}
}
Thanks,
Greg
#!/usr/bin/perl -s
my $start_point=".";
&Traverse($start_point);
sub Traverse{
my ($workdir) = shift;
opendir(DIR, $workdir) or die "Unable to open $workdir:$!\n";
my @dirarray = readdir(DIR) or die "Unable to read $workdir:$!\n";
&PrintFileInfo($workdir);
foreach my $name (@dirarray){
next if $name eq ".";
next if $name eq "..";
if(-d $name){
&Traverse($name);
} else {
&PrintFileInfo($name);
}
}
}
sub PrintFileInfo{
my ($file) = shift;
if(-d $file){
print "DIRECTORY $file\n";
} else {
print "\tFILE $file\n";
}
}
Thanks,
Greg