[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Problem with FSpIterateDirectory



At 11:02 AM -0700 1999-08-09, Rob Sirota wrote:
>I am having a bit of a problem when using FSpIterateDirectory. I have a
>top level folder that contains a lot of subfolder and files, I wish to
>delete all file and folders inside this top level folder. I am getting
>strange results:
>
>When I call:
> FSpIterateDirectory($SDKVol,
>      0,
>      sub {
>       local($temp) = @_;
>       if (-f $temp) {
>        print "File: ", $temp, "\n";
>        }
>       return 0;
>      },
>      "");
>
>every file prints out, however if I put the delete in the sub some files
>are skipped:
>
> FSpIterateDirectory($SDKVol,
>      0,
>      sub {
>       local($temp) = @_;
>       if (-f $temp) {
>        print "File: ", $temp, "\n";
>        FSpRstFLock($temp) or warn $^E;    # Unlock the file.
>        FSpDelete($temp);    # Delete the file.
>       }
>       return 0;
>      },
>      "");
>
>if I only have the FSpRstFLock() to unlock the file I see all of the
>files..
>
>Any suggestions?

If you were to index all the files in each directory, its only the
odd-numbered ones that get deleted, right?  If so, the problem you're
having is most probably that FSpIterateDirectory is relying on the
numbering to remain static for the duration of the iteration -- which, if
you're deleting files, it's not.  When it runs the second time, it operates
on file 2, which used to be file 3 before you deleted file 1.  The original
file 2 becomes file 1 (when the original file 1 is deleted), and remains.

If memory is not a problem, you can use:

sub {
    my ($temp) = @_;
    if (-f $temp) {
        push @FILES, $temp;
    }
}

as the filter, and after the FSpIterateDirectory call, simply do:

foreach my $file (@FILES) {
    FSpRstFLock($file) or warn $^E;    # Unlock the file.
    FSpDelete($file);    # Delete the file.
}

This could pose a memory problem if you have a huge directory structure.

If that doesn't suit you, you'll probably have to get an iterator that
handles dynamic indexing (possibly writing it yourself).  Or, if all you
want to do is delete everything, you can just delete item 1 until you get
fnfErr, and then delete the empty directory.  If you get 'directory not
empty' on an item (it's a directory), then recurse into the directory.

Or maybe you just have the FSpIterateDirectory-doesn't-delete-all-the-files
virus. :-)

Josh

--
Joshua Juran                          Metamage Software Creations
=)                                         Tools for Wizards
wanderer@metamage.com
<http://www.metamage.com/>   * Creation at the highest state of the art *



===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org