Copying files across folders using Perl on Windows

Posted on 2004-11-13
Last Modified: 2010-03-05

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.
Question by:jmohsin
    1 Comment
    LVL 84

    Accepted Solution

    use File::Copy;
    use File::Compare;
    for( <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 $!";
            unlink or warn "delete $_ $!";

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video discusses moving either the default database or any database to a new volume.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now