Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

deleting a file

by Anonymous Monk
on Jun 04, 2001 at 19:19 UTC ( #85496=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

in perl, is there any function to delete a file?

Comment on deleting a file
Re: deleting a file
by zigster (Hermit) on Jun 04, 2001 at 19:22 UTC
Re: deleting a file
by Cirollo (Friar) on Jun 04, 2001 at 19:22 UTC
Re: deleting a file
by shotgunefx (Parson) on Jun 04, 2001 at 19:24 UTC
    No unfortunately not. But you know what they say about JAPHs, they have big hard drives ;)

    Come on, of course there is! Wouldn't be much of a language without a way of deleting files. Just go to search and type "delete file" and you'll find all kinds of help.

    -Lee

    "To be civilized is to deny one's nature."
Re: deleting a file
by JSchmitz (Canon) on Jun 04, 2001 at 20:29 UTC
    Use unlink - something like this although this is for files with a specific numeric naming convention
    print "Enter starting file name: "; $start = <>; print "Ending file name: "; $end = <>; chomp($start); chomp($end); opendir(MDSDIR,"/mds/nvision1"); while ( $file = readdir(MDSDIR) ) { if ($file =~ /000[0-9A-C]*\./) { $temp = $&; chop($temp); if ( $temp ge $start && $temp le $end ){ $temp = "/mds/nvision1/" . $temp . ".$'"; print "unlinking $temp\n"; unlink($temp); } } } closedir(MDSDIR);
Re: deleting a file
by snafu (Chaplain) on Jun 04, 2001 at 23:14 UTC
    unlink

    $cnt = unlink 'a', 'b', 'c'; unlink @goners; unlink <*.bak>;

    ----------
    - Jim

Re: deleting a file
by particle (Vicar) on Jun 04, 2001 at 23:44 UTC
    How about something that uses the output of unlink for a good purpose? no sense in letting it go to waste, after all...
    @files = qw/file1 file2 file3/; $cnt = unlink(@files); unless( scalar(@files) == $cnt ) { die("Problem unlinking $files[$cnt]! $!"); }
    also, this tells you which file was the problem, e.g.
    Problem unlinking file2! No such file or directory at del.pl line 4.
    there's always a better way. is there something better here?

    Update 2002-06-19: yes, there's a better way. the above code assumes too much about the return value from unlink.

    you can't reliably determine which file(s) failed to unlink from the return code alone. i suggest the following modification, which will report accurate results:

    unless( $cnt == @files ) { die 'Problem unlinking ', $cnt, ' of ', scalar(@files), ' files! ', +$!; }
    =Particle
Re: deleting a file
by Anonymous Monk on Jun 05, 2001 at 09:25 UTC
    If you are writing a perl program that needs to unlink a file and also run on VMS then try to keep track of how many distinct times you have opened it for write or append access then unlink the same number of times. The reason for doing so is that the VMS file system provides automatic file versioning, hence if you
    open FH ">foo.txt"; print FH "blah" close FH; open FH1 ">foo.txt"; print FH1 "blah" close FH1; unlink "foo.txt"; # `DELETE FOO.TXT;2` if ($^O eq 'VMS') { unlink "foo.txt" } # `DELETE FOO.TXT;1`
    Of course if it is difficult or impossible to know how many times a file has been opened for writing or appending (such as often is the case with temp files or regression test output cleanup) then there is a strange little idiom that works on VMS and all other perl implementations (such as Unix):
    1 while unlink $filename;
    Gets rid of it.
(boo) Re: deleting a file
by boo_radley (Parson) on Jun 06, 2001 at 16:45 UTC
      Heh, I did not post the original node in this thread. :)

      The point of my exercise on comp.lang.perl.misc, as Andrew describes on use.perl.org, was to show that a newbie could ask a basic question about Perl ("How do I delete a file?") in a way that would generate helpful answers and not flames ("I found a function called delete in the documentation, but it doesn't seem to do what I want.").

      As you can see, the Anonymous Monk here was not careful about how the question was asked. :) But note, even though the question got voted way down, it's a testament to the Monastery that the Anonymous Monk nonetheless received several helpful answers and no flames.

      I'd completely forgotten about my pseudo-newbie posting to clpm until Andrew reminded me of it a few days ago. I think it occurred four or five years ago. Unfortunately, the archives at http://www.deja.com seem to go back only to spring of 1997.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2014-09-02 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (32 votes), past polls