Welcome to the Monastery, NoobCoder!

Use hippo's advice. This code doesn't do what I quickly put it together to do...

I've rearranged a few things and made some subtle changes. I've also used File::Find::Rule as it's more flexible (allows you to specify wildcards, and doesn't require a callback). I also use File::Basename, as basename digs out only the file name and throws away the path part. There are most definitely better ways to do this than this code I've put together quickly, but it'll give you some ideas.

I started with a directory structure like this:

Original 'from' dir:

test/ run1.log run2.log run3.log

...and the copy-to dir:

test2/ run2.log run2.log_1 run2.log_2

Now, in the code, what I've done, is create a recursive subroutine that checks if the original file is already in the destination directory, and if it is, it appends a _N to the end, where N is an integer. It iterates continuously, and will use the first available filename.log_N that is available:

use warnings; use strict; use File::Basename; use File::Copy; use File::Find::Rule; my $count = 1; my $dir = 'test'; my $to_dir = 'test2'; my @files = File::Find::Rule->file() ->name('*.log') ->in($dir); if (@files){ for (@files){ if ($_ =~ /run/i){ my_copy($_); } } } sub my_copy { my ($base_file, $file_name) = @_; # get just the filename my $file = basename $base_file; # $file_name will be empty on the initial call... # let's set it so it's not undef $file_name = $file if ! $file_name; if (-e "$to_dir/$file_name"){ # strip off the _N suffix if present $file_name =~ s/_\d+$//; # append a new _N, then increment the count $file_name = "${file}_$count"; $count++; my_copy($base_file, $file_name); } else { print "copying $base_file to $to_dir/$file_name\n"; copy $base_file, "$to_dir/$file_name" or die $!; } }

With the example directories above, the result is this:

test2/ run1.log run2.log run2.log_1 run2.log_2 run2.log_3 run3.log

On second pass:

test2/ run1.log run1.log_1 run2.log run2.log_1 run2.log_2 run2.log_3 run2 +.log_4 run3.log run3.log_1

In reply to Re: File::Copy renaming trouble by stevieb
in thread File::Copy renaming trouble by NoobCoder

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.