Click here to get back home

Batch Renaming a group of files in a directory.

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Batch Renaming a group of files in a directory. George Watson 11-28-2007
Posted by George Watson on November 28, 2007, 1:33 am
Please log in for more thread options
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.

Think this is a pipe dream or is it feasible?

Please provide the code in your response along with explanation.

Thanks in advance for your help.



Posted by John W. Krahn on November 28, 2007, 3:44 am
Please log in for more thread options
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

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



Posted by Ilya Zakharevich on November 28, 2007, 11:19 pm
Please log in for more thread options
[A complimentary Cc of this posting was sent to
George Watson
> 300006784-2007-11-27-22-40.dat to new name of 6784-071127-3.drt

pfind .
"s/^\d(\d)-20(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)\.dat$/$1-$2$3$4-$5-$6.dat/"

will not do "-3" part the way you want; but if you omit -$5-$6 part,
it would automatically uniquefy the name (in a similar way).

Hope this helps,
Ilya

Posted by George Watson on November 29, 2007, 1:58 am
Please log in for more thread options
Thanks for the input. I shall try it later.


> [A complimentary Cc of this posting was sent to
> George Watson
>> 300006784-2007-11-27-22-40.dat to new name of 6784-071127-3.drt
>
> pfind .
>
"s/^\d(\d)-20(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)\.dat$/$1-$2$3$4-$5-$6.dat/"
>
> will not do "-3" part the way you want; but if you omit -$5-$6 part,
> it would automatically uniquefy the name (in a similar way).
>
> Hope this helps,
> Ilya



Similar ThreadsPosted
New module for renaming files within an editor October 27, 2004, 6:53 pm
WriteExcel module set_row and set_column to group by both January 20, 2005, 2:40 pm
Image Magick thumbnail batch resizing January 16, 2006, 12:46 pm
Announce: CafeBatch -- a batch file whose scope is network wide-- assessing interest August 2, 2005, 10:11 am
Portable dot-files (hidden-files) ? November 5, 2007, 1:20 am
Traverse a directory July 13, 2006, 2:10 pm
Glob and Traversing Directory July 14, 2006, 12:13 pm
New Script Directory needs your scripts listed September 3, 2005, 6:32 pm
Modules not installing to standard LIB directory November 17, 2005, 8:14 am
Current Directory when test.pl runs ? December 14, 2004, 9:31 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap