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

Re: deleting a file in perl

by 2teez (Priest)
on Apr 02, 2013 at 06:44 UTC ( #1026594=note: print w/ replies, xml ) Need Help??


in reply to deleting a file in perl

Something like this using File::Find, which IMHO is an overkill for what you need,
since you are just going through one directory, you could consider using opendir, if you want.

use warnings; use strict; use File::Find qw(find); die "No directory is specified" unless defined $ARGV[0]; my $dir = $ARGV[0]; find( \&unwanted, $dir ); sub unwanted { if (/^\.{1,}/) { return } else { print "Do you want to delete ", $_, $/; chomp( my $ans = <STDIN> ); unlink $_ if $ans =~ /\by\b/; } }
NOTE: Deleting a file could be both dangerous and so painful if in error, a wrong file is deleted, hence the need to check the file before deleting.
Hope this helps.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re: deleting a file in perl
Download Code
Re^2: deleting a file in perl
by keralaqueen234 (Initiate) on Apr 02, 2013 at 06:57 UTC
    could you please tell me how exactly do i do the coding for this? i need to first see which all files in the diectory has the file named currentlog*.log . and i need to delete all the files with that name pattern,but need to retain currentlog.log(which is the latest log file ) ..pleasee help me :(

      keralaqueen234,
      The script shown in my previous post, should work.
      Usage: perl delete_log.pl <directory_name>
      Where delete_log.pl is your Perl script, directory_name the directory you which to go through, checking which file to delete, after you have been shown the name of the file, if you want to delete the file you answer y, if not just press ENTER.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

        yes that works .. but in my folder xyz , i have files with names currentlog.log,currentlog123.log,currentlog233.log,detail.txt,see.txt .... i need to delete only the files which has the name pattern of 'currentlog'. other files including currentlog.log need to be retained.how do i find the patern of filenames ?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026594]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-07-26 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls