gitarwmn has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I need to be able to get a list of directories all ending with a numeric value and then copy the latest directory into a new directory incrementing the number by 1. Anyone have any advice on how to do that?
Thanks

Replies are listed 'Best First'.
Re: copy directory
by ikegami (Patriarch) on Sep 28, 2006 at 22:51 UTC
Re: copy directory
by graff (Chancellor) on Sep 29, 2006 at 01:10 UTC
    A couple more things that might be handy additions to ikegami's list:

    grep
    -M

    Having a better definition of the problem would help, too. Does "latest directory" refer reliably to the one having the highest numeric value in its name? Are the numeric values supposed to be "fixed-width" (padded with leading zeros where needed)? These are minor details, but they do have an impact on what would make a proper solution.

    Try something out; if it doesn't do what you want, and you can't figure out where you went wrong, post what you tried, with enough information about some actual file names so we can repeat (and maybe fix) your experiment.

Re: copy directory
by Adrade (Pilgrim) on Sep 29, 2006 at 02:05 UTC
    I don't think I even totally understand your question, but if I do, this is how I would sort of quickly approach it. It's not beautiful, but it works.
      -A
    my $container = '/path/to/dir/that/holds/all/the/dirs'; my $tomove = '/path/to/the/dir/you/want/to/move'; my $newdir; chomp($newdir = `ls -1 $container | tail -n 1`); $newdir++; system("mv $tomove $container/$newdir");
    Edit: I just noticed you want to copy instead of move - in that case, just replace the last line with system("cp -r $tomove $container/$newdir");

    --
    By a scallop's forelocks!

Re: copy directory
by Cristoforo (Curate) on Sep 29, 2006 at 23:04 UTC
    In addition to the advice by the others, maybe this link on copying directories might be helpful.

    Chris

Re: copy directory
by fmerges (Chaplain) on Sep 30, 2006 at 15:08 UTC

    Hi,

    In addition to ikegami and graff, I would suggest using File::Path, it has something like mkpath that creates directory hierarchies in one simple step...

    Regards,

    fmerges at irc.freenode.net