P
Peter Jamieson
I have inherited many .rtf files contained in a directory structure like:
patient_data/patient1/2007_07_11/test1.rtf
where there are many patient directories such as patient1, patient2 etc
and many date directories such as 2007_07_11, 2007_07_09 etc
and there may be many .rtf files per patient and date combination.
What I would like to do is create new unique names for each .rtf file
based on the directory names: eg patient1_2007_07_11.test1.rtf
and put them into a new directory named patient_files.
I have checked out File::Find and tested the following script.
It lists all the files OK as expected like test1.rtf, test2.rtf etc
It merely replaces "_" with " " just as a test script.
However I cannot figure out how to do a rename for what
I would like the new file names to be namely in the
form of: patient1_2007_07_11.test1.rtf
Any help appreciated!, Cheers Peter
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
my $dir = '/patient_data/patient';
find(\&underscores, $dir);
sub underscores {
next if -d $_;
next if /^\./;
next unless /_/;
my $new_name = $_;
$new_name =~ s/_/ /g; # just for testing!
chdir($File::Find::dir);
rename($_, $new_name) or die $!;
print "$new_name \n";
}
patient_data/patient1/2007_07_11/test1.rtf
where there are many patient directories such as patient1, patient2 etc
and many date directories such as 2007_07_11, 2007_07_09 etc
and there may be many .rtf files per patient and date combination.
What I would like to do is create new unique names for each .rtf file
based on the directory names: eg patient1_2007_07_11.test1.rtf
and put them into a new directory named patient_files.
I have checked out File::Find and tested the following script.
It lists all the files OK as expected like test1.rtf, test2.rtf etc
It merely replaces "_" with " " just as a test script.
However I cannot figure out how to do a rename for what
I would like the new file names to be namely in the
form of: patient1_2007_07_11.test1.rtf
Any help appreciated!, Cheers Peter
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
my $dir = '/patient_data/patient';
find(\&underscores, $dir);
sub underscores {
next if -d $_;
next if /^\./;
next unless /_/;
my $new_name = $_;
$new_name =~ s/_/ /g; # just for testing!
chdir($File::Find::dir);
rename($_, $new_name) or die $!;
print "$new_name \n";
}