Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

File deletion

by cored (Scribe)
on Nov 01, 2002 at 03:29 UTC ( [id://209629]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, i want to make a script that delete files i mean if a make this
@file = qw ( /bin/ls /bin/bash );
i only want that the files that are in the array dosent get delete and the files that aren't get delete thx

Replies are listed 'Best First'.
Re: File deletion
by Zaxo (Archbishop) on Nov 01, 2002 at 03:46 UTC

    unlink is the builtin function you want.

    unlink @files;

    After Compline,
    Zaxo

      Except it sounds like you want to delete everything BUT the contents of the array.

      my %keep; @keep{@files} = 1; # populate a lookup hash foreach my $file ( @filelist ) { unlink $file unless exists( $keep{$file} ); }

        There's one minor problem with your example: your hash slice assignment creates the expected keys, so exists will work, but since you're assigning only one value to the slice, only the hash element with the key corresponding to the first element in @files will have a value (1), the rest will have the undefined value. This could lead to confusion (i.e. code that mysteriously refuses to work the way you expect it to) later on should you be required to make any changes that assume that each element of the hash has the value 1.

        It would probably be best to either assign an empty list to the hash slice, which would give you a key for each filename in @files for use with exists, or to assign a list of true values with the same number of elements as the @files array using the repetition operator x.

        @keep{@files} = (); # empty list @keep{@files} = (1) x @files; # list of "TRUE" values
        With the latter assignment, you could modify the code to check the value of the hash element, instead of the key:
        @keep{@files} = (1) x @files; foreach my $file ( @filelist ) { unlink $file unless $keep{$file}; }
        I think it would be a whole lot easier to get a hash of the files in the directory. Then delete the files to be saved out of the hash? And then unlink all files in the hash? ex.
        my %trash; @trash{@file_list} = " "; delete @trash{@saved_files}; foreach $file (keys %trash) { unlink $file; }
        Tell me if I am wrong but woulnt it be a bit faster because you are not doing a conditional check in the loop?
        Three
        But, this will erase the file that is in the hd right ?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found