Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Delete files with Perl in Windows

by whnwhrwhy (Novice)
on Nov 29, 2007 at 22:59 UTC ( #654000=perlquestion: print w/ replies, xml ) Need Help??
whnwhrwhy has asked for the wisdom of the Perl Monks concerning the following question:

I have a cgi script with a Perl sub routine under Windows which gives me trouble deleting files from a directory. I have tried various routines and still cannot delete files from the directory.
sub ClearForm { @filelist = glob("C:/Program Files/Apache Group/Apache2/htdocs/frames/ +*.jpg"); unlink @filelist; print "<center><h2>All old frames have been deleted</h2></center><p>\n +"; &PrintForm; }
Thanks!!!

20071130 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Comment on Delete files with Perl in Windows
Download Code
Re: Delete files with Perl in Windows
by moritz (Cardinal) on Nov 29, 2007 at 23:07 UTC
    Are you sure that the script has enough privileges to delete these files?

    Usually CGI scripts are run with low privileges in order to prevent malicious actions if there are security holes, and usually they can't delete arbitrary files.

Re: Delete files with Perl in Windows
by ikegami (Pope) on Nov 29, 2007 at 23:12 UTC

    You need to escape spaces in a glob spec unless you want them to be treated as pattern seperator.

    >perl -le"print for glob '*.jpg *.gif'" a.jpg b.jpg c.gif >perl -le"print for glob 'C:/Documents and Settings/*'" C:/Documents and >perl -le"print for glob 'C:/Documents\ and\ Settings/*'" C:/Documents and Settings/All Users C:/Documents and Settings/Default User C:/Documents and Settings/ikegami C:/Documents and Settings/kaijugal C:/Documents and Settings/LocalService C:/Documents and Settings/NetworkService

    Alternatively, use bsd_glob from File::Glob.

    >perl -le"use File::Glob qw( bsd_glob ); print for bsd_glob 'C:/Docume +nts and Settings/*'" C:/Documents and Settings/All Users C:/Documents and Settings/Default User C:/Documents and Settings/ikegami C:/Documents and Settings/kaijugal C:/Documents and Settings/LocalService C:/Documents and Settings/NetworkService
      If you don't want to set the escapes manually, then quotemeta is your friend.
      perl -le "print for glob quotemeta q(C:/Documents and Settings/*)"

      print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

        Sorry, but that doesn't work. The reason it doesn't work depends on the system.

        • On non-dosish systems, that will escape meta-characters that should remain meta-characters, such as *.

          That means glob quotemeta $expr is the same as ''.$expr.

        • On dosish systems, that will insert characters that will be interpreted as directory separators.

          On DOSISH systems, [...] backslashes (under GLOB_QUOTE) only quote the glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself. All other backslashes are passed through unchanged.

          That means glob quotemeta 'c:/some dir/*.*' is the same as glob 'c:/some dir/*/.*'.

Re: Delete files with Perl in Windows
by tachyon-II (Chaplain) on Nov 29, 2007 at 23:15 UTC

    $! check the return value. As noted this is probably a permissions issue. Check the helpful me$!$!age. Make sure you have printed a header then.....

    for my $file (@filelist) { unlink $file or print "<p>Unable to delete $file. Reason $!\n"; }
Re: Delete files with Perl in Windows
by TGI (Vicar) on Nov 30, 2007 at 19:09 UTC

    Not related to your question, but do you really mean &PrintForm?

    Calling a subroutine with the leading & and no arguments passes the calling sub's argument list. Since Perl passes sub arguments by reference, this can lead to some spooky action at a distance that can be very hard to debug. See perlsub for more details.

    Consider this code:

    my $critical_data = 'Original Value'; print "Begin: $critical_data\n"; &SubOne($critical_data); print "End: $critical_data\n"; sub SubOne { print "\tSubOne Got: @_\n"; &SubTwo; } sub SubTwo { print "\tSubTwo Got: @_\n"; $_[0] = 'pwned!'; } __END__ Begin: Original Value SubOne Got: Original Value SubTwo Got: Original Value End: pwned!

    Compare the result of this code:

    my $critical_data = 'Original Value'; print "Begin: $critical_data\n"; SubOne($critical_data); print "End: $critical_data\n"; sub SubOne { print "\tSubOne Got: @_\n"; SubTwo(); } sub SubTwo { print "\tSubTwo Got: @_\n"; $_[0] = 'pwned!'; } __END__ Begin: Original Value SubOne Got: Original Value SubTwo Got: End: Original Value

    Which would you rather debug?


    TGI says moo

Re: Delete files with Perl in Windows
by Anonymous Monk on Dec 04, 2007 at 03:37 UTC
    File::Remove...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-09-21 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls