(jeffa) Re: how to rename all files in a dir
by jeffa (Bishop) on Jul 07, 2002 at 03:25 UTC
|
These things i tend to do with quick one-liners. Say you
want to prepend the number to all .txt files:
perl -e 'while(<*.txt>){$n=sprintf("%02d%s",++$i,$_);rename$_,$n}'
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)
| [reply] [d/l] |
|
In case there is no specified file extension like .txt, something like this can be done:
perl -e 'for (grep {-f} <*>) { rename $_, sprintf("%02d%s", ++$i, $_) }'
Zenon Zabinski | zdog | zdog@perlmonk.org
| [reply] [d/l] [select] |
Re: how to rename all files in a dir
by flounder99 (Friar) on Jul 07, 2002 at 04:18 UTC
|
File::Copy has a handy move function. The most important thing to remember is that readdir does not return the path just the files in the specified dir. So you have to remember tack the path back on before doing anything else like file tests or renaming.
use strict;
use File::Copy qw ( move );
my $dirname = "./temp"; # dir you want to open;
my $iterator = 0;
opendir (DIRHANDLE, $dirname) or die "could not open dir: $!";
foreach (readdir(DIRHANDLE)) {
#need to add path
my $filename = $dirname . "/$_";
next unless -f $filename;
move ($filename, $dirname . "/" . $iterator++) or warn "could not
+rename file";
}
-- flounder | [reply] [d/l] [select] |
|
Having been bitten by the "unadorned filename" return of readdir (isn't every newbie?), I got to wondering if there is ever the situation where you do not want the the full path? If so, how often, relative to the times when you do?
I guess it's way to late to consider that this might be changed, given the golden rule that "Thou shalt not break old code".
However, as it seems that Perl6 is going to comprehensively break this rule - maybe a new default would be in order?
| [reply] |
|
If so, how often, relative to the times when you do?
While I share your pain (and frequently stumb my toe on
this behavior), I think this idiom will help
you. Use the chdir() function to get to the desired directory,
then read away. That way, your can use the result of readdir
directly.
use CWD;
my $org_dir = cwd;
chdir($desired_dir) || die "can't cd: $!";
opendir(DIR, $desired_dir) || die "can't opendir: $!";
while(my $file = readdir DIR){
next if -d $file;
# preform operations below on $file
}
closedir DIR;
chdir($org_dir) || die "can't get back to $org_dir: $!";
| [reply] [d/l] |
|
| [reply] |
|
Its not a matter of not wanting the full path... readdir() can't really give you the full path.
It take a DIRHANDLE as an argument, and there is no fool-proof way of getting the correct "full path" from a DIRHANDLE.
-Blake
| [reply] [d/l] |
|
The real poison pill here is symlinks. Let's say you chdir to a location on your file system and read the listings there. Perl has no way of knowing if you came to that directory through 'hard' links or symlinks. And what is returned by pwd from the command line may or may not match up with what that command will return within perl.
So, the answer to your question is, you probably DO want the full path in many situations, but there is no way to automatically get there from within a script. You will usually end up putting in some code that forces Perl to use a particular directory location.
oakbox
| [reply] |
Re: how to rename all files in a dir
by Juerd (Abbot) on Jul 07, 2002 at 10:15 UTC
|
I want to... A snippet of code would help
Hire a programmer or learn. Although many seem to think so, PerlMonks is no "Free custom scripts" site.
If you want help, state a question, and let us know what you have already tried yourself. Make sure you have RTFM and used strict and warnings. Don't ask for code, ask for help. You'll probably get code anyway.
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
| [reply] |