Re: Checking a folder again after it has been emptied
- From: ahamlin@xxxxxxxxx
- Date: 14 Feb 2007 09:26:19 -0800
Always begin your script with these two lines:
use strict;
use warnings;
They will save you a lot of problems.
You will be forced to declare your variables (using 'my'), so declare
them
when you need them.
Yes, I know, it was sloppy code. The code below includes those.
You could definitely clean up your program by making those repetitive
loops
in a subroutine.
I like to get the logic working, then I'll optimize my code. I know
it will become a problem as the code gets larger, but it's still a
small enough program that I don't have to worry about that yet. Now
that I have it working, I can write 4 calls to a subroutine that runs
that while loop.
I believe you are going to have to re-open the directory after it has
been emptied to determine if new files have been written.
Thanks, that worked. I have it open the directories at the beginning
of the initial while loop and will read the first priority folder
again. I closed the directories at the end of their respective
loops. I figured this was a good idea, but is there a penalty to
leaving the directories open and the loop keeps reopening them?
Also, I have trouble with the following line (and those like it):
while ((defined ($file1=readdir(PRV_SRC_1))) && $i<$num_file)
I believe the conditions should be reversed. It seems to me that when
i is equal to $num_file, you have still read a file name from the
directory. Yet the loop is not entered, so the file is never moved
(unless you reopen the directory and catch these files that were left
behind, rewinding the directory handle would also work, but you would
also get the files already moved.).
I wanted to make sure files were in the directory before checking to
see if the files should be moved or not. The program started off much
simpler with moving files from just one directory to another and no
additional directories. The logic works because $i gets reset by the
bedtime function, so once it's moved 5 files, it sleeps and resets $i,
allowing additional files in that folder to be moved and now with your
help if I add files to that folder after it has cleared that folder,
it will moves those as well.
That did indeed help, thanks again.
HTH, Ken
Paul:
I tried to get the directory to rewind, but was unable to get it to
ignore the files it has already moved. I could have read the files it
moved into an array and had it check to make sure it skipped that file
before trying to move it, but Ken's solution was simpler. Thank you
for your suggestion.
As for a simplier code, here it is below and it works:
use strict;
use warnings;
use File::Copy;
##########################
#Variable declaration #
##########################
#file and folders used
my $file1;
my $dir_src_1="c:/test/source/1/"; #priority 1 source
my $dir_dst_1="c:/test/dest/1/"; #priority 1 destination
my $file2;
my $dir_src_2="c:/test/source/2/"; #priority 2 source
my $dir_dst_2="c:/test/dest/2/"; #priority 2 destination
#sleep timer and number of files
my $nap=15; #sleep timer
my $num_file=5; #number of files to move
#counters
my $i=0; #num_file counter
my $j=0; #total number of files counter
##########################
# Open files and folders #
##########################
#Create/open log file and error log file
open (LOG, ">>c:/test/dev/logfile.txt") or die "Can't open logfile: $!
\n";
#Open priority 1 destination directory
opendir(PRV_DST_1, $dir_dst_1) or die "Can't open $dir_dst_1 : $!\n";
#Open priority 2 destination directory
opendir(PRV_DST_2, $dir_dst_2) or die "Can't open $dir_dst_2 : $!\n";
#########################
# Begin main program #
#########################
#Using j to limit it to 30 files total moved for testing
LINE: while ($j<30)
{
opendir(PRV_SRC_1, $dir_src_1) or die "Can't open $dir_src_1 : $!
\n";
opendir(PRV_SRC_2, $dir_src_2) or die "Can't open $dir_src_2 : $!
\n";
##################################
#Read each file in each directory #
##################################
#priority folder 1
while ((defined ($file1=readdir(PRV_SRC_1))) && $i<$num_file)
{
next if $file1 =~ /^\.\.?$/; # skip . and ..
#Move the currently selected file
print LOG scalar localtime(), " Moving $file1 from $dir_src_1 to
$dir_dst_1\n";
move("$dir_src_1$file1","$dir_dst_1$file1") or warn "Can't move
$file1 from $dir_src_1 to $dir_dst_1: $!\n";
$i++;
$j++;
#program will run until counter hits num_file, then will sleep for
15 seconds before resetting counter and moving onto next file
if ($i==$num_file)
{
bedtime($i,$j);
}
closedir PRV_SRC_1;
redo LINE; #attempt to get program to start at top of loop to check
time and hopefully go back to priority one directory
}
#priority folder 2
while ((defined ($file2=readdir(PRV_SRC_2)) && $i<$num_file))
{
next if $file2 =~ /^\.\.?$/; # skip . and ..
#Move the currently selected file
print LOG scalar localtime(), " Moving $file2 from $dir_src_2 to
$dir_dst_2\n";
move("$dir_src_2$file2","$dir_dst_2$file2") or warn "Can't move
$file2 from $dir_src_2 to $dir_dst_2: $!\n";
$i++;
$j++;
#program will run until counter hits num_file, then will sleep for
15 seconds before resetting counter and moving onto next file
if ($i==$num_file)
{
bedtime($i,$j);
}
closedir PRV_SRC_2;
redo LINE;
}
}
print LOG "\nFiles finished moving $j files on ", scalar localtime(),
"\n";
#Close out the directories
closedir PRV_SRC_1;
closedir PRV_DST_1;
closedir PRV_SRC_2;
closedir PRV_DST_2;
####################################
# End of main program #
####################################
sub bedtime
{
print "\nGoing to sleep now\n";
print LOG "\nBreak on ", scalar localtime(), " after moving $i files
and $j total files\n";
sleep $nap;
$i=0;
print "\nThat was a nice nap, back to work\n";
print LOG "Starting transfer again on ", scalar localtime(), "\n\n";
return $i;
}
.
- Follow-Ups:
- Re: Checking a folder again after it has been emptied
- From: Paul Lalli
- Re: Checking a folder again after it has been emptied
- From: Uri Guttman
- Re: Checking a folder again after it has been emptied
- References:
- Checking a folder again after it has been emptied
- From: ahamlin
- Re: Checking a folder again after it has been emptied
- From: kens
- Checking a folder again after it has been emptied
- Prev by Date: Re: Is Perlmonks.org down? Or is it me?
- Next by Date: Re: Checking a folder again after it has been emptied
- Previous by thread: Re: Checking a folder again after it has been emptied
- Next by thread: Re: Checking a folder again after it has been emptied
- Index(es):
Relevant Pages
|