Perl script to move partial file list

Here is some sample Perl code I hacked together to solve an interesting problem today. We are building an E-commerce web site for a client. This client was given a DVD with over 22,000 product images images equalling over 1 GB of data. However he only wanted to use around 750 of these images. We had a list of file names that he wanted to use and the DVD.

This bit of Perl code looks at the list of images we created with one file name per line and copies these images only to a new directory. Then we simply moved over the images in our new directory to the web server.

#!/usr/bin/perl
use File::Copy;
# establish variable list
my $movefile;
my @movelist;
# this is the path that we copy from
my $srcpath = '/source/path/here';
# this is the path that we copy to
my $movepath = '/destination/path/here/';
my $line;
# this is the file that has the list of files
open (MOVEFILE, '/Path/to/filelist.txt') or die "problem open";
while ( $line = <MOVEFILE> ) {
chomp $line;
push (@movelist, split(/\r/, $line ));
}
foreach $movefile(@movelist){
print "$srcpath$movefile"." — ". "$movepath$movefile"."\n";
system("cp $srcpath$movefile $movepath$movefile");
}

I found these two sample sites that helped in formulating this solution:

http://forums.hostmysite.com/post-6651.html

http://www.bradrice.com/wposx/archives/59

Let us know in the comments if you find this code sample helpful.


One response to “Perl script to move partial file list

  1. Python version (not tested)
    #!/usr/local/bin/python
    import os,sys
    source = 'path/to/source'
    target = 'target/path'
    for movethis in open(listfile).read().splitlines():
        command = 'cp %(source)s/%(movethis)s %(target)s/%(movethis)s' % vars()
        os.system(command)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.