Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: File deletion

by Rahul6990 (Beadle)
on May 06, 2013 at 12:02 UTC ( [id://1032261] : note . print w/replies, xml ) Need Help??

in reply to File deletion


It is very easy in Perl.Use the below steps:

use strict; my $file_location = shift; open(FH,">","log.txt") or die "unable to open file log.txt"; my @files = <$file_location/*>; foreach my $file (@files){ system("rm $file"); unless( $? == -1 ){ print FH "\n$file is deleted.."; } else{ print FH "\n$file is not deleted.. $!"; } } close FH;

Thanks marto for pointing out the error , updated the code, now working correctly.

Replies are listed 'Best First'.
Re^2: File deletion
by marto (Cardinal) on May 06, 2013 at 12:16 UTC

    "It is very easy in Perl"

    Your code doesn't even run.

    Can't take log of 0 at line 2

    open. You also don't check for failures when opening the log file.

Re^2: File deletion
by marto (Cardinal) on May 06, 2013 at 12:19 UTC

    Also $file doesn't contain what you assume it does, hence all your deletes fail when you prepend the path.

Re^2: File deletion
by thezip (Vicar) on May 06, 2013 at 18:29 UTC


    It is considered polite around these parts when, if you're going make updates to your posted code, that you indicate the before and after versions of the code. This would be more illustrative and will give others the opportunity to benefit from your mistakes so that they might not have to make them later.

    At the very least, you should give a brief description of the changes you've made, so that some semblance of context/continuity remains.


    What can be asserted without proof can be dismissed without proof. - Christopher Hitchens, 1949-2011