|
Posted by George Watson on November 29, 2007, 1:57 am
Please log in for more thread options Thanks for the input. I will play with this later.
> George Watson wrote:
>>
>> I am trying to write a perl script to rename files from one format to
>> another.
>>
>> example:
>>
>> 300006784-2007-11-27-22-40.dat to new name of 6784-071127-3.drt
>>
>> 300006784-2007-11-27-17-30.dat to new name of 6784-071127-1.drt
>>
>> 300006784-2007-11-27-20-26.dat to new name of 6784-071127-2.drt
>>
>> I want to delete the first 5 digits 300006784 to 6784
>>
>> The date 2007-11-27 to 071127
>>
>> I want to move compare the last 4 digits representing time (22-40, 17-30
>> &
>> 20-26) time hacks and make it either 1, 2 or 3 depending on the time it
>> was
>> created.
>>
>> the naming convention for all the files will effectively have the same
>> format in the directory. Number of files to rename could be from 1 to
>> 150
>> or more.
>
>
> Something like this should work:
>
>
> my $dir = '/some/dir';
>
> opendir my $dh, $dir or die "Cannot open '$dir' $!";
>
> while ( my $file = readdir $dh ) {
> next unless $file =~
> /\A\d(\d-)\d\d(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)\.dat\z/;
> $files{ "$1$2$3$4" }{ "$5$6" } = $file;
> }
>
> for my $file ( keys %files ) {
> my $count = 1;
> for my $time ( sort { $a <=> $b } keys %{ $files{ $file } } ) {
> my $new_file = "$file-" . $count++ . '.drt';
> rename "$dir/$files", "$dir/$new_file"
> or die "Cannot rename '$files' $!";
> }
> }
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
|