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

I have a script that compiles a bunch of files into one, and then saves the compiled file. I decided I wanted to delete all of the small files that make up the big one when I am done to keep everything nice and clean. I am running into a problem, however. Here is the code:
$pres->SaveAs("$filename"); #pres is a PP presentation #lets clean up the directory #@files is a list of all filenames I use +d to create the overall file. foreach $file (@files){system("del \"$file\"")};
The problem is that my files won't delete, because they are still open by PowerPoint. I tried to put a sleep after the SaveAs, but then it paused prior to the Save! This doesn't seem to make any sense to me. Can anyone explain why this is occuring. Thanks,
~~David~~

Replies are listed 'Best First'.
Re: Odd Timing Executing Lines of Code
by Zaxo (Archbishop) on Feb 27, 2007 at 16:58 UTC

    You could retain @files until you shut down PP, and then delete them. I recommend perl's unlink builtin over system del.

    unlink or warn $! for @files;
    or if you don't care about which fail, just,
    unlink @files;

    After Compline,
    Zaxo

Re: Odd Timing Executing Lines of Code
by holli (Abbot) on Feb 27, 2007 at 17:01 UTC
    Do you call the close (or similar named) method of the presentation object? Also, you'd better use the unlink function to avoid unneccessary shelling out.


    holli, /regexed monk/
      You are right, closeing it worked. I tried close on the main file, but I had to close the individual files as well. Thank you,
      ~~David~~
Re: Odd Timing Executing Lines of Code
by jettero (Monsignor) on Feb 27, 2007 at 17:02 UTC

    You can maybe save yourself some trouble by not forking to the system shell like that.

    First of all, if you use the multi arg version system you'll have less interpolation trouble to avoid (e.g., system("del", $file)==0 or die).

    Second, try out unlink. It probably does that better anyway. I'm not a windows guy (which is the only platform where this kind of timing "issue" is a problem), but I think you can do something like this:

    for my $file (@files) { my $retries = 2; JUST_TRY_ME: { unless( unlink $file ) { if( (--$retries) > 0 ) { warn "problem unlinking file ($file): $!"; sleep 2; redo JUST_TRY_ME; } else { die "too many problems unlinking file ($file) ..."; } }

    UPDATE: holi is probably right... you probably just need to close first.

    -Paul

Re: Odd Timing Executing Lines of Code
by diotalevi (Canon) on Mar 01, 2007 at 02:22 UTC

    I'd be looking at Win32API::File's use of CreateFile and the FILE_FLAG_DELETE_ON_CLOSE flag. If your plain unlink doesn't work because the file is opened exclusively for PowerPoint, you can try opening it with this other flag. This flag is then set on the open handle so whenever PowerPoint exits, the file gets deleted. That's the basic idea I've gathered from other people who have dealt with this sort of problem on Windows.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊