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

Hey Monks,

I can't seem to find what is wrong with this script,
use strict; my $data = "C:\\dir"; opendir DIR, $data; my @file = grep {/^APPL_RMKS_RCV.200309.*$/ } readdir DIR; closedir DIR; #This produces a screen printout of a number of files in the directory + with filenames matching the initial search print @file; unlink @file or die "$!";

The error I am getting is "No such file or directory." I know that I have the correct filenames, because of the print @file. I've tried doing it one file at a time in a for loop getting individual filenames in the array, with the same result. Any ideas? Thanks.

Replies are listed 'Best First'.
Re: Deleting Files
by broquaint (Abbot) on Oct 03, 2003 at 15:27 UTC
    You're providing unlink with an incomplete path i.e missing $data. A quick map should rememdy this
    unlink map "$dir\\$_", @file == @file or die "ack: $!";
    Oh and the .*$ in your regex is extraneous as you're matching zero or more of anything to the end of the string, which will always match.
    HTH

    _________
    broquaint

    update: fixed bug (added == @file to die appropriately), thanks to liz

Re: Deleting Files
by chromatic (Archbishop) on Oct 03, 2003 at 15:27 UTC

    readdir returns file names without directory paths. You're probably not in C:/dir, so you need to give your files absolute paths or change to that directory.

    use File::Spec; my @files = map { File::Spec->catfile( 'c:/dir', $_ ) } grep { ... } readdir DIR;
      Thanks very much! I guess I was just looking at the stupid code for too long, couldn't see the simple solution!! Thanks again.
Re: Deleting Files
by liz (Monsignor) on Oct 03, 2003 at 15:23 UTC
    unlink() returns the number of files that are deleted. If you want to check whether all files are deleted, something like:
    my $deleted = unlink @file; die "Only deleted $deleted out of ".@file." files.\n" if $deleted != @ +file;
    would be more appropriate.

    Liz

Re: Deleting Files
by dragonchild (Archbishop) on Oct 03, 2003 at 15:29 UTC
    Try replacing the following:
    unlink @file or die "$!"; --- unlink "$data\\$_" || die "$!" for @file;

    Your problem is that you're not in the directory the file is in, so your unlink is failing. (Worse is that you might delete the wrong file!)

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Deleting Files
by chanio (Priest) on Oct 04, 2003 at 05:27 UTC
    I would try using chdir() to get into that directory.

    ActivePerl says...

    'It‘s probably worth mentioning that if you‘re going to filetest the return values out of a readdir, you‘d better prepend the directory in question. Otherwise, because we didn‘t chdir() there, it would have been testing the wrong file.'