Re: Checking a folder again after it has been emptied



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.

HTH, Ken
That did indeed help, thanks again.


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;
}


.



Relevant Pages

  • Checking a folder again after it has been emptied
    ... -Folders have a priority. ... I am able to start with priority folder 1, ... sleep for 15 seconds, both of which can be changed. ... closedir PRV_SRC_1; ...
    (perl.beginners)
  • Re: Problem with my code
    ... This declaration is being done too soon. ... This also brings up another problem with your code: you don't sleep ... inside the loop. ... closedir $dh; ...
    (perl.beginners)
  • Re: Outlook 2003 vba macro
    ... Don't move or delete items in a For Each loop! ... 'Move the email Item to the backup folder ... Microsoft Outlook Programming - Jumpstart for Administrators, Power Users, and Developers ... > Dim objApp As Application ...
    (microsoft.public.outlook.program_vba)
  • Re: Help with a long running process?
    ... Before the loop enters its initial cicle, it will first have to grab ... all the files from all sub folders off the base path you specified. ... String)) or return it as an array. ... consider loading the files folder by folder: ...
    (microsoft.public.dotnet.languages.vb)
  • Re: problems deleting email messages in c#
    ... Deleting items from a collection requires either a count down for loop or another looping construct that tests for the collection being empty. ... Hard-deleting an item (bypassing Deleted Items) is only possible using CDO 1.21 or Extended MAPI or a COM wrapper for MAPI such as Redemption. ... An alternative is to handle ItemAdd on the Items collection of the Deleted Items folder. ... I am putting together a personal project to help me remove spam out of my ...
    (microsoft.public.office.developer.outlook.vba)