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

hello: i was wondering how you could delete files in a directory. i tried the following codes, but none seem to work. got ne ideas why? what am i doing wrong. thanks.
opendir(ims, "/home/tanglam/www/$Account::cata_acc/_data/messages"); @imss = grep !/\.\.?/, readdir MESSAGE; closedir(ims); foreach $imfile (@imss) { unlink("/home/tanglam/www/$Account::cata_acc/_data/messages/$imfile") +|| print "Error deleting message"; }


this one too
opendir(ims, "/home/tanglam/www/$Account::cata_acc/_data/messages"); @imss = grep !/\.\.?/, readdir MESSAGE; unlink(@imss); closedir(ims);

2001-03-25 Edit by Corion : Closed final CODE tag

Replies are listed 'Best First'.
Re: deleting a list of files
by Anonymous Monk on Mar 25, 2001 at 12:42 UTC
    Your grep should have !/^\.\.?$/ rather than !/\.\.?/. Your pattern will match any filename with a dot in it, which results in any file with a dot in its name will not be in @imss, which is probably why your files aren't being deleted. /^\.\.?$/ will match only '.' and '..' which I believe is what you are trying to do.

        I'm curious: I've always just used /^\.\.$/, assuming that it would be sufficient. Are you recommending /\A\.\.\z/ as a defensive programming practice or are there specific situations you've run into where you needed to do this?

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