http://qs321.pair.com?node_id=1032261


in reply to File deletion

Hi,

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 orig.pl 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

    Rahul6990,

    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.

    </broken_record>



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