Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

File deletion

by 14sudharsan (Initiate)
on May 06, 2013 at 10:48 UTC ( [id://1032240]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Team,

Im new to perl scripting. I have thousands of file names like below i have pasted.

rm 20130119-154557_9417560919_rubika_CANARARE-all.mp3 rm 20130122-151714_9003230528_desigans_RELINST-all.mp3 rm 20130128-110435_9865934829_gowrih_CLUBNEW-all.mp3 rm 20130119-172714_9994641778_gowrih_CLUBNEW-all.mp3 rm 20130110-150044_9833539373_ezhil_CANARARE-all.mp3 rm 20130131-094051_8754558656_selvakumarir_BAXANEW-all.mp3 rm 20130117-154031_9884040442_karthikae_CLUB-all.mp3 rm 20130110-100956_9952927800_bhagatsinghd_RELINST-all.mp3 rm 20130107-184842_9841118822_karthikae_CLUB-all.mp3 rm 20130102-162259_9789096778_babysheelaa_CLUB-all.mp3 rm 20130123-145333_9841169173_Anandkumarc_RELINST-all.mp3 rm 20130125-161408_9841274078_karthikae_CLUB-all.mp3 rm 20130130-094413_9840454410_prithivrajb_RELINST-all.mp3 rm 20130104-133223_2030578020_karthick_DB748-all.mp3 rm 20130107-145805_9884020568_jesinthamary_REL-INS-all.mp3 rm 20130102-162826_9566118210_dhanasekarank_RELINST-all.mp3

while i run this command from a text file bash -v /path/textfile

it will delete

now i want to write a log file for what are all the files deleted success full...

as of now im running this task daily in bash script ... kindly guide me how to do in perl script step by step

Thanks is advance

Replies are listed 'Best First'.
Re: File deletion
by blue_cowdawg (Monsignor) on May 06, 2013 at 13:09 UTC

    Here's something to try:

    #!/usr/bin/perl ############################################################## use strict; my $directory=shift @ARGV; # get the target directory open $fh,"> /tmp/logfile.txt" or die "/tmp/logfile.txt:$!"; if ( ! -d $directory ) { puke_and_die($fh,"no such directory: " . $directory); } chdir $directory or puke_and_die($fh,"Cannot chdir to $directory"); opendir(DIR,$directory); while(my $fname=readdir(DIR)){ next if ($fname eq '.') || ($fname eq '..'); next if -d $fname; unlink $fname or logit ($fh,"could not remove " . $fname . ": $ +!"); } exit(0); sub logit { my($fh,$msg) = @_; printf $fh,"%s\n",$msg; } sub puke_and_die { my ($fh,$msg)=@_; logit($fh,$msg); close $fh; exit(-1); }
    now:
    1. This assumes you are passing the directory you want cleaned out as an argument to the script. If not you need to change the mechanism a bit to accommodate your requirements.
    2. This is completely untested and is the product of a caffeine deficient brain,

    ENOCOFFEE: SysAdm Halted. Brain fried; core dumped.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: File deletion
by Rahul6990 (Beadle) on May 06, 2013 at 12:02 UTC
    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.

      "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.

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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-25 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found