Untested code coming...
#!/usr/bin/perl
my $pattern = "PGP";
&renameFiles('/home/user/perl/testincoming');
sub renameFiles {
# Pass directory name as parameter
my $Dir = shift;
opendir(DIR, $Dir) || die "Can't opendir $Dir: $!";
my @Files = grep { -f "$Dir/$_" } readdir(DIR);
rewinddir(DIR);
my @Dirs = grep { /^[^.].*/ && -d "$Dir/$_" && ! -l "$Dir/$_"} rea
+ddir(DIR);
closedir DIR;
foreach $file (@Files) {
open(FILE,"<$Dir/$file") || die "Can't open $file: $!";
my $doRename=0;
READFILE: while (<FILE>) {
if (/\Q$pattern\E/) {
$doRename=1;
last READFILE;
}
}
close(FILE);
if ($doRename) {
my $newname=$file;
$newname=~s/^./o/;
rename ("$Dir/$file", "$Dir/$newname");
}
}
# Call self for each subdir
foreach $SubDir (@Dirs) { &renameFiles(join("/",$Dir,$SubDir)); }
};
Should rename all files containing a PGP line in the whole subdirectory tree.
This is just a quick modify of something I already had, so it may contain bugs, etc...
I'm not responsible for what it does to your system, no warraty, etc, etc. You know the standard phrases.
UPDATE:As
merlyn humbly suggests in his post, I now check for symlinks with && ! -l "$Dir/$_". The code is still
untested from my part.
/brother t0mas