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

Hello, I have the following probleme with ActiveState Perl 5.6.1 on W2K Pro. With my script I read the contents of diffrent text files with an foreach loop. Within that loop I open each file with open and at the end I use close() to close the file handle. The next I do is to unlink the files. Everything works fine except the last file does not get deleted. I get an error message "Permission denied" and that an other process is on it. Any hints, Govinda Pfister

Replies are listed 'Best First'.
(tye)Re: unlink problem
by tye (Sage) on Dec 05, 2001 at 20:48 UTC

    The message "The process cannot access the file because it is being used by another process" is rather misleading. It should really drop the "by another process" part because it can easily be the same process that is "using" the file and preventing it from being deleted.

    See (tye)Re: unlink & rename in Win32 (from two days ago; did you look around or even search/Super Search?) for more background on this error. (:

            - tye (but my friends call me "Tye")
Re: unlink problem
by CubicSpline (Friar) on Dec 05, 2001 at 18:33 UTC
    This could be a couple of things. First off, are you sure that there really isn't another process using the file? If not that, I wonder if it might be an off-by-one type problem with your algorithm. How does it go? Is it like:

    foreach $file (@files) { open FILE, $file; . . . close FILE; unlink $file; }

    or is it something else? Just out of curiosity, is it always the same file that cannot be deleted or is it the last file that you access? If the latter is the case, I'd look for an algorithm problem. Perhaps you could post your code here( and be sure to use <code> tags around it).

    ~CS