samuelj123
samuelj123

Reputation: 67

Perl: How can I periodically move certain files from one folder to another?

I need to move files that have a certain file-name from a source folder to its specific destination folder to another in alphabetical order one by one every, say, 5 minutes.

this is what i've come up with so far...

#!/usr/bin/perl
use strict;
use warnings;
my $english = "sourcepath";
my $destination = "destination path";

#for(;;)
#{
opendir(DIR, $english) or die $!;
while (my $file = readdir(DIR))
    {
    next unless (-f "$english/$file");
    next unless ($file =~ m/english/);
    move ("$english/$file", "$destination");
    }
closedir (DIR);
#sleep 10;
#}  
exit 0;

The problem now, is that I am not able to move them alphabetically one by one... Any pointers? Thanks

Upvotes: 0

Views: 677

Answers (2)

mzedeler
mzedeler

Reputation: 4371

In stead of

while (my $file = readdir(DIR))

You can get the files in alphabetical order like so:

for my $file (sort readdir(DIR))

Thumbs up for using strict and warnings. Consider reading up on perlstyle for tips on how to properly format the code.

Upvotes: 2

choroba
choroba

Reputation: 242218

If you want to process the files in alphabetical order, sort them. To get their list, you can use readdir in list context:

opendir(DIR, $english) or die $!;
my @files = sort readdir DIR;
for my $file (@files) {
    # ....
}

Upvotes: 2

Related Questions