[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Copying files across folders using Perl on Windows

Posted on 2004-11-13
Medium Priority
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 85

Accepted Solution

ozo earned 2000 total points
ID: 12577335
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans
Suggested Courses

873 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