Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Problem unlinking files

by compskian (Initiate)
on Nov 16, 2007 at 13:54 UTC ( [id://651201]=perlquestion: print w/replies, xml ) Need Help??

compskian has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Problem unlinking files
by toolic (Bishop) on Nov 16, 2007 at 14:07 UTC
    Please show your code so that we may see the order in which you are validating and unlinking.

    Remember to use code tags for your code, as described in Writeup Formatting Tips.

Re: Problem unlinking files
by GertMT (Hermit) on Nov 16, 2007 at 14:10 UTC
    maybe you didn't do close FILE; before you unlink it?
Re: Problem unlinking files
by compskian (Initiate) on Nov 16, 2007 at 14:33 UTC
    Just to add to the information. What i want actaually is a refreshing of the directory while its undergoing the 'readdir while loop', if some deletion of files happens in this while loop.
      Without seeing your code, this is just a guess...

      I assume that now you are doing something like this:
      opendir(DIR, "$somedir") or die $!; while(my $dirEntry = readdir(DIR)){ if(&SOME_TEST($dirEntry)){ unlink $dirEntry; unlink $dirEntryPair; } } closedir(DIR);


      If in fact this is what you're doing, why don't you try another approach:
      opendir(DIR, "$somedir") or die $!; my @dir = readdir(DIR); foreach my $dirEntry (@dir){ if(&SOME_TEST($dirEntry)){ unlink $dirEntry; unlink $dirEntryPair; @dir = readdir(DIR); } } closedir(DIR);
        Regarding this part of your suggested code:
        foreach my $dirEntry (@dir){ if(&SOME_TEST($dirEntry)){ ... @dir = readdir(DIR); } }
        As a rule, it is a Bad Idea™ to alter or replace the contents of an array within a "for" loop that is iterating over that array. Results of doing so may be "unpredictable" at best.

        As it turns out, checking for existence of a file before doing "unlink" is actually unnecessary -- you are not checking the return status from "unlink", and frankly, why care whether you "unlink" a file that is non-existent? That sort of "error" is harmless and could safely be ignored.

        Say, why do you have the ampersand before 'SOME_TEST' -- &SOME_TEST(...) -- but not anywhere else? Followup: why have an ampersand at all (in your proposed code)?
      Just to add to the information. What i want actaually is a refreshing of the directory while its undergoing the 'readdir while loop', if some deletion of files happens in this while loop.

      Why do you want to "refresh" the directory (and what do you mean by that, anyway)? It would have been better if you had added a sample of the code you have tried.

      Maybe rather than deleting files within the loop, you might want to just build a list of files to be deleted (store the file names as keys in a hash), and then delete them as a separate step:

      opendir(DIR,$somepath); my %todelete; while( $file = grep /[^.]/, readdir DIR ) { if ( !exists( $todelete{$file} ) and some_test( "$somepath/$file" +)) { $todelete{$file} = undef; ( my $otherfile = $file ) =~ s/this/that/; $todelete{$otherfile} = undef; } } unlink "$somepath/$_" for ( keys %todelete );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://651201]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found