Delete a file : unlink « File « Perl






Delete a file

      

    #!/usr/local/bin/perl -w

    use Getopt::Long;

    my $ret = GetOptions ("f|filename:s");
    my $filename = $opt_f || die "Usage: $0 -f filename\n";

    if (-e $filename)
    {
       # Delete the file.
       if (unlink ($filename))
       {
          print "The file $filename has been deleted.\n";
       }
       else
       {
          print "The file $filename was not deleted: $!\n";
       }
    }
    else
    {
       print "The file $filename does not exist.\n";
    }

   
    
    
    
    
    
  








Related examples in the same category

1.The unlink function deletes a list of files on both UNIX and Windows systems.
2.Delete every file whose name ends in .bak: