Hi WisDomSeeKer34,

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:

my @names = ('a'..'zz');
so that you can be sure you'll not run out of names.

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):

my $name = 'a'; for my $file_in (@files) { rename $file_in, $name; $name++; }
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:
for my $file_in (@files) { rename $file_in, "$directory/$name"; $name++; }
HTH.

In reply to Re: Renaming files in directory by Laurent_R
in thread Renaming files in directory by WisDomSeeKer34

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.