in reply to Renaming files in directory
If you just want to rename files, then I would suggest that you just use the rename built-in function of Perl, rather than using the File::Copy module.
Also, I would suggest that the glob function is, in at least 95% of the cases, much easier to use than the opendir/readdir combination for at least three reasons: it is simpler (one code line instead of two), it returns the filenames with their path (so you don't have to add the path yourself), and it removes special "files" such as . and .. from the file list.
Then, unless you can really be sure that your file list will always have less than 16 files, don't hard code your list as you did. Increment you file names as you go, or build a much larger list, for example:
so that you can be sure you'll not run out of names.my @names = ('a'..'zz');
Finally, using a "c-style" loop is probably not a good idea (and it is really not perlish). You could try something like this (assuming you made the other changes I mentioned, especially glob):
Then, there is a slight problem: you don't say clearly where you want the output files to be created. Probably you want to first cd to the directory, or else want to change the loop above to:my $name = 'a'; for my $file_in (@files) { rename $file_in, $name; $name++; }
HTH.for my $file_in (@files) { rename $file_in, "$directory/$name"; $name++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Renaming files in directory
by Anonymous Monk on Jan 05, 2018 at 23:42 UTC | |
by Laurent_R (Canon) on Jan 06, 2018 at 09:31 UTC | |
by WisDomSeeKer34 (Sexton) on Jan 06, 2018 at 20:04 UTC | |
by WisDomSeeKer34 (Sexton) on Jan 12, 2018 at 10:30 UTC | |
by afoken (Chancellor) on Jan 12, 2018 at 12:27 UTC |