Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

deleting a file

by Anonymous Monk
on Jun 04, 2001 at 19:19 UTC ( [id://85496] : perlquestion . print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: deleting a file
by zigster (Hermit) 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.


    "To be civilized is to deny one's nature."
Re: deleting a file
by Cirollo (Friar) on Jun 04, 2001 at 19:22 UTC
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 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 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! ', +$!; }
Re: deleting a file
by snafu (Chaplain) on Jun 04, 2001 at 23:14 UTC

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

    - Jim

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, 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 seem to go back only to spring of 1997.