I am applying my regex here:
foreach my $dir(@subdirs){
opendir my $dh, "$topdir/$dir" or die "Error: $!";
my @files = readdir $dh;
print "raw files = @files\n";
@files = grep /^(\d+)(.+)(LacZ|pgK|SD|SU)$/,@files;
print "$dir -> @files\n";
foreach my $file(@files){
system( "mv $topdir/$dir/$file $newdir/$file");
}
closedir $dh;
}
I am only getting the subdir under "/home/mgavi/testdir1/RESULTS 2008". Do I have to grep "/home/lomspace/testdir1/RESULTS 2008/APRIL 2008/Results 09-02-08D"?
I am confused on using grep to get to the files here. There dirs for each month and under each month there are many "Results" files.
Your wisdom can lead to my enlightenment.
| [reply] [d/l] |
When you readdir a directory, you will get a list of files AND subdirs. You need to test the list, and see if any of the results match -d or -f for dir or file. If it matches -d, then run the sub again on it.....it's called recursion.
#!/usr/bin/perl
sub get_sub_dirs {
my $dir = shift;
opendir my $dh, $dir or die "Error: $!";
my @files = grep !/^\.\.?$/, readdir $dh;
closedir $dh;
for my $file ( @files ) {
print "$dir/$file\n";
get_sub_dirs( "$dir/$file" ) if -d "$dir/$file";
#notice the -d test, saying it found deeper subdirs
}
}
$topdir= shift || '.';
get_sub_dirs($topdir);
exit;
| [reply] [d/l] |