Copying files across folders using Perl on Windows


I want to accomplish the following using Perl on a Windows XP machine.

Consider the following folder/file structure:

folder fo1 has files fi1, fi2, and fi3
folder fo2 has files fi3, fi4, and fi5

I want to copy and store all files in fo2.  Think of fo1 as a temp folder.  If the file names are unique, there is no problem.  However, if there are overalpping file names, I want to make a decision.  In the example above, here is how fo2 will look like AFTER the script has been run: fi1, fi2, fi3, fi4, and fi5.  Folder fo1 will be empy.  However, when file fi3 is moved from fo1 to fo2, a check should be done to determine if the two version of fi3 are the same.  If the fo1.fi3 and fo2.fi3 are identical, fo1.fi3 should be deleted as the file fo2.fi3 is already there.  However, if fo1.fi3 and fo2.fi3 are DIFFERENT, first of all fo2.fi3 should be left alone. fo1.fi3 should be RENAMED to a filename that does NOT exist in folder fo2 and THEN moved to folder fo2.  The renaming can be a simple scheme (e.g. append _1, or _2 to the end of the filename).

Does this clarify what I am trying to do ?

In reality, I have hundreds of files spread across tens of folder.
jmohsinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
use File::Copy;
use File::Compare;
for( <fo1/*> ){
   my($f)=/^fo1\/(.*)/;
   if( !-e "fo2/$f" ){
        move($_,"fo2/$f") or warn "$_,fo2/$f $!";
   }elsif( compare($_,"fo2/$f") ){
        my $n=1;
        ++$n while -e "fo2/${f}_$n";
        move($_,"fo2/${f}_$n") or warn "$_,fo2/${f}_$n $!";
   }else{
        unlink or warn "delete $_ $!";
   }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.