Copying and deleting files in multiple directories with perl.

Now I came out with a simple program for doing this. I was faced with a situation where I had to copy a file in 20 different folders in order to test some code that I was writing. I also had to change data and copy the file again many times. I figured that this process would kill me. I wanted to automate this process so that i don’t have to struggle further. Initially I played with Batch scripting.. never liked it 😦 And all of sudden I remembered about my old time dabbling with perl.. now that flash of idea came in as a real perl from the sky!. Here is the code:

#!c:/Perl/bin/perl.exe
print “Which file to copy: “;
$fl = <>;
chomp($fl);
use File::Copy;
opendir DIR, “.”; # . is the current directory

while ( $filename = readdir(DIR) )
{
if(-d $filename && $filename ne ‘.’ && $filename ne ‘..’)
{
print “Copied $fl to: ” , $filename,”\n”;
copy($fl,$filename);
}
}

Here I list the directories using ‘opendir’ on the current directory (here is where my 20 folders were). You could also take an input for this from the user. And for each item in the list returned, I copy the file in that directory with a ‘copy’ function. I filtered the “.”, “..” from the list for I don’t need to copy my files there. The “-d” checks for directory.

In the same manner, we can also delete files from the directories:

#!c:/Perl/bin/perl.exe
print “Which file to delete: “;
$fl = <>;
chomp($fl);
opendir DIR, “.”; # . is the current directory

while ( $filename = readdir(DIR) )
{
if(-d $filename && $filename ne ‘.’ && $filename ne ‘..’)
{
print “Deleted $fl in: ” , $filename,”\n”;
unlink(“$filename\\$fl”);
}
}

#observer the unlink function here. You have to specify the path of the file for each iteration. “\\” is a slash with an escape.

Advertisements

4 Responses to “Copying and deleting files in multiple directories with perl.”

  1. Hi,
    Ii need to create directories which is a mixture of variables and number and then I need to copy the file and give the respective folder name to the file.
    But I have some problem in the
    loop as the file is copied to all my directories and it is unable to
    copy the z.txt to the subdirectories. eg: /abc/FD, pqt/FD, /sde/FD.

    #!\usr\bin\perl
    use warnings;
    mkdir(“F:/usr/ eg/abc”, 0777) || print $!;
    mkdir(“F:/usr/ eg/abc/FD” , 0777) || print $!;
    mkdir(“F:/usr/ eg/pqt”, 0777) || print $!;
    mkdir(“F:/usr/ eg/pqt/FD” , 0777) || print $!;
    mkdir(“F:/usr/ eg/sde”, 0777) || print $!;
    mkdir(“F:/usr/ eg/sde/FD” , 0777) || print $!;
    print “Enter the file to copy: “;
    $fl = ;
    chomp($fl);
    use File::Copy;
    opendir DIR, “.”; # . is the current directory

    while ( $filename = readdir(DIR) )
    {
    if(-d $filename && $filename ne’.’ && $filename ne ‘.’)
    {
    print “copied $fl to: ” , $filename,”\ n”;
    copy($fl,$filename) ;
    }
    }
    rename(‘F:/usr/ eg/abc/FD/ z.txt’,’F: /usr/eg/abc/ FD/abc.txt’ );
    rename(‘F:/usr/ eg/pqt/FD/ z.txt’,’F: /usr/eg/pqt/ FD/pqt.txt’ );
    rename(‘F:/usr/ eg/sde/FD/ z.txt’,’F: /usr/eg/sde/ FD/sde.txt’ );

    Could you please tell me how to correct the loop.

    Regards,
    Ramesh

  2. vinaychilakamarri Says:

    you have to fix your while loop there. The while loop that I wrote is meant for copying the file into all the directories immediately under ‘.’ and not the sub directories under them. But i suggest you have a look at this module: http://search.cpan.org/~tty/kurila-1.11_0/lib/File/Find.pm

    for all dirs under .
    {
    go recursively till the end of dir
    copy file
    }

  3. serm…

    […]Copying and deleting files in multiple directories with perl. « A coder’s blog for coders[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: