Discussion:
renaming files
Adriano Allora
2006-06-30 14:48:09 UTC
Permalink
hi to all,

does exists a way to rename automatically files?

I mean: I've got directories with this content:

EN0.tmp
EN1.tmp
EN2.tmp
EN3.tmp
EN4.tmp
...
IT0.tmp
IT1.tmp
IT2.tmp
IT3.tmp
IT4.tmp
...

Now, some couples of files were deleted (you can imagine the
EN1.tmp/IT1.tmp) but I need no holes in my list.

Is there something less rude (and more general) than

$c = 0;
while(<>)
{
if($ARGV=~/^([A-Z]+)(\d+)\.tmp/)
{
$nuname = $1.$c.'.tmp';
`mv $ARGV $nuname';
$c++;
}
}

Thanks at all,

alladr

|^|_|^|_|^| |^|_|^|_|^|
| | | |
| | | |
| |*\_/*\_/*\_/*\_/*\_/* | |
| |
| |
| |
| http://www.e-allora.net |
| |
| |
**************************************
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Mr. Shawn H. Corey
2006-06-30 15:03:41 UTC
Permalink
Post by Adriano Allora
hi to all,
does exists a way to rename automatically files?
No.

See:
perldoc -f rename
perldoc -f glob
perldoc File::Find
perldoc File::Copy (and search for "move")
--
__END__

Just my 0.00000002 million dollars worth,
--- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Dr.Ruud
2006-06-30 18:33:44 UTC
Permalink
Post by Mr. Shawn H. Corey
Post by Adriano Allora
does exists a way to rename automatically files?
No.
perldoc -f rename
perldoc -f glob
perldoc File::Find
perldoc File::Copy (and search for "move")
And of course: IO::All.
http://search.cpan.org/search?module=IO::All
--
Affijn, Ruud

"Gewoon is een tijger."
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jeff Peng
2006-06-30 16:03:21 UTC
Permalink
Hello.
Instead of `mv..` system call,you could use perl's inner function 'rename()'
to do the things you wanted.
Also use 'File::Find' to do the searching for files.
Subject: renaming files
Date: Fri, 30 Jun 2006 16:48:09 +0200
hi to all,
does exists a way to rename automatically files?
EN0.tmp
EN1.tmp
EN2.tmp
EN3.tmp
EN4.tmp
...
IT0.tmp
IT1.tmp
IT2.tmp
IT3.tmp
IT4.tmp
...
Now, some couples of files were deleted (you can imagine the
EN1.tmp/IT1.tmp) but I need no holes in my list.
Is there something less rude (and more general) than
$c = 0;
while(<>)
{
if($ARGV=~/^([A-Z]+)(\d+)\.tmp/)
{
$nuname = $1.$c.'.tmp';
`mv $ARGV $nuname';
$c++;
}
}
Thanks at all,
alladr
|^|_|^|_|^| |^|_|^|_|^|
| | | |
| | | |
| |*\_/*\_/*\_/*\_/*\_/* | |
| |
| |
| |
| http://www.e-allora.net |
| |
| |
**************************************
--
<http://learn.perl.org/> <http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Rob Dixon
2006-06-30 18:44:21 UTC
Permalink
Post by Adriano Allora
hi to all,
does exists a way to rename automatically files?
EN0.tmp
EN1.tmp
EN2.tmp
EN3.tmp
EN4.tmp
...
IT0.tmp
IT1.tmp
IT2.tmp
IT3.tmp
IT4.tmp
...
Now, some couples of files were deleted (you can imagine the
EN1.tmp/IT1.tmp) but I need no holes in my list.
Is there something less rude (and more general) than
$c = 0;
while(<>)
{
if($ARGV=~/^([A-Z]+)(\d+)\.tmp/)
{
$nuname = $1.$c.'.tmp';
`mv $ARGV $nuname';
$c++;
}
}
[snip sig}

Hi Adriano

This isn't the answer you wanted. Depending on your point of view you may
consider it even ruder than your code, but the 'automatic' way that you wanted
doesn't exist :-/

You appear to have an input file with a list of files that need to be renamed.
I've just grabbed a listing of the current directory, and fixed the problems that:

- You may well want the files numbered in the same /order/ as they originally
were, which won't be the order they appear in a directory listing.

- It looks like you want a separate numbering sequence for each filename prefix.

This does all those things, and is deliciously rude:

opendir my $dh, '.' or die $!;

my %pfx;
foreach (
sort { ($a =~ /(\d+)/)[0] <=> ($b =~ /(\d+)/)[0] }
grep /^[A-Z]+\d+\.tmp$/, readdir $dh) {
/^([A-Z]+)/;
(my $new = $_) =~ s/\d+/$pfx{$1}++ || 0/e;
rename $_, $new;
}

Hope this helps or amuses.

Rob
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Continue reading on narkive:
Loading...