Batch Renaming a group of files in a directory.
Batch Renaming a group of files in a directory.
am 28.11.2007 07:33:57 von George Watson
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.
Re: Batch Renaming a group of files in a directory.
am 28.11.2007 09:44:36 von krahnj
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{5}(\d{4}-)\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{$file}{$time}", "$dir/$new_file"
or die "Cannot rename '$files{$file}{$time}' $!";
}
}
John
--
use Perl;
program
fulfillment
Re: Batch Renaming a group of files in a directory.
am 29.11.2007 01:21:16 von zachscrivena
i have written an open source command-line program (RenameWand at
http://renamewand.sourceforge.net/) that does this and more;
unfortunately it's in Java (reply offline if you're still interested)
Zach Scrivena
http://zs.freeshell.org/
Re: Batch Renaming a group of files in a directory.
am 29.11.2007 05:19:17 von Ilya Zakharevich
[A complimentary Cc of this posting was sent to
George Watson
], who wrote in article :
> 300006784-2007-11-27-22-40.dat to new name of 6784-071127-3.drt
pfind . "s/^\d{5}(\d{4})-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
Re: Batch Renaming a group of files in a directory.
am 29.11.2007 07:57:17 von George Watson
Thanks for the input. I will play with this later.
"John W. Krahn" wrote in message
news:474D2A56.BE69F33D@telus.net...
> 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{5}(\d{4}-)\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{$file}{$time}", "$dir/$new_file"
> or die "Cannot rename '$files{$file}{$time}' $!";
> }
> }
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
Re: Batch Renaming a group of files in a directory.
am 29.11.2007 07:58:15 von George Watson
Thanks for the input. I shall try it later.
"Ilya Zakharevich" wrote in message
news:filek5$obi$1@agate.berkeley.edu...
> [A complimentary Cc of this posting was sent to
> George Watson
> ], who wrote in article
> :
>> 300006784-2007-11-27-22-40.dat to new name of 6784-071127-3.drt
>
> pfind .
> "s/^\d{5}(\d{4})-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
Re: Batch Renaming a group of files in a directory.
am 29.11.2007 07:58:41 von George Watson
Thanks for the help,
wrote in message
news:7ce5fadb-5d4e-434e-8e6e-83ff06616db2@e23g2000prf.google groups.com...
>i have written an open source command-line program (RenameWand at
> http://renamewand.sourceforge.net/) that does this and more;
> unfortunately it's in Java (reply offline if you're still interested)
>
> Zach Scrivena
> http://zs.freeshell.org/