For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2007 > Re: Checking a folder again after it has been emptied









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Checking a folder again after it has been emptied
kens

2007-02-15, 9:59 pm

On Feb 15, 9:36 am, "Paul Lalli" <mri...@gmail.com> wrote:
> On Feb 14, 12:26 pm, aham...@gmail.com wrote:
>
>
>
> Really? Seems to work just fine for me:
>
> $ rm temp/*
> $ touch temp/A.txt temp/B.txt
> $ perl -le'
> opendir my $dh, "temp" or die $!;
> while (my $file = readdir($dh)) {
> if (-f "temp/$file") {
> print "Unlinking $file";
> unlink "temp/$file" or die $!;
> }}
>
> if (fork() == 0){
> open my $ofh, ">", "temp/C.txt" or die $!;
> print $ofh "Hey I am a new file!";
> close $ofh;
> exit;}
>
> sleep(5); #parent sleeps 5 seconds to let the child work;
> my @old_files = readdir($dh);
> rewinddir($dh);
> my @new_files = readdir($dh);
> print "Before rewinding: @old_files";
> print "After rewinding: @new_files";
> '
> Unlinking A.txt
> Unlinking B.txt
> Before rewinding:
> After rewinding: . .. C.txt
>
> Paul Lalli


Paul,
He is running on Windows, and I think rewinddir must behave
differently as opposed to Unix. I tried your code (with the exception
of 'fork' and exit):

> opendir my $dh, "temp" or die $!;
> while (my $file = readdir($dh)) {
> if (-f "temp/$file") {
> print "Unlinking $file";
> unlink "temp/$file" or die $!;
> }}
>
> # if (fork() == 0){
> open my $ofh, ">", "temp/C.txt" or die $!;
> print $ofh "Hey I am a new file!";
> close $ofh;
> # exit;}
>
> sleep(5); #parent sleeps 5 seconds to let the child work;
> my @old_files = readdir($dh);
> rewinddir($dh);
> my @new_files = readdir($dh);
> print "Before rewinding: @old_files";
> print "After rewinding: @new_files";


Or was the fork necessary. Got an error trying to use it on Windows.

Anyway, for the code I used, after rewinding it reported ". .. A.txt
B.txt"

Ken

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com