<?xml version="1.0" encoding="windows-1252"?>
<node id="944153" title="is this file open?" created="2011-12-17 22:50:08" updated="2011-12-17 22:50:08">
<type id="115">
perlquestion</type>
<author id="326962">
thnidu</author>
<data>
<field name="doctext">
Honored monks:
&lt;p&gt;I use Perl under WinXP and Mac OS X, but this question is specifically for WinXP (although I may want to port the script to Mac somedayP.&lt;/p&gt;

&lt;p&gt;This script, &lt;c&gt;index.pl&lt;/c&gt;, opens the current directory with the filehandle &lt;c&gt;POSTED&lt;/c&gt; and looks for filenames beginning with a certain pattern, 
(&lt;c&gt;/^(\d{6}[b-z]?) /&lt;/c&gt;). It analyzes each such filename with a series of regexps, then writes the results of its analysis for each filename to an output file (handle &lt;c&gt;OUT&lt;/c&gt;) as tab-delimited text.&lt;/p&gt; 

&lt;p&gt;When it's done all such files -- when the &lt;c&gt;while(($file = readdir(POSTED))) {...}&lt;/c&gt; loop is done -- it closes &lt;c&gt;OUT&lt;/c&gt; and calls &lt;c&gt;system&lt;/c&gt; to open it in NotePad.&lt;/p&gt;

&lt;p&gt;Then I manually "select all" in NotePad, "copy", and close the file. The script then opens an Excel spreadsheet into which I paste the data for further manipulation. Here's the main code:&lt;/p&gt;

&lt;c&gt;   open(OUT, '&gt;', 'index.txt') or die($!);
   &amp;byfile; # subroutine to find, analyze, &amp; write filenames
   printf STDERR ("%d lines processed\nwith header, " .
      "%d lines written to index.txt", $lines, $lines + 1);
   close(OUT);
   system("notepad index.txt");
   system("index.xlsx");&lt;/c&gt;

&lt;P&gt;Here's the problem: I often run this script iteratively to make sure that new files are getting treated properly, or when tweaking the program. At such times, I &lt;i&gt;already&lt;/i&gt; have the spreadsheet open from the previous iteration. The script works all right, but I keep getting error messages on the terminal window: "The process cannot access the file because it is being used by another process." Not only is this a nuisance, but the script has wasted time (I don't know if any significant amount) in vainly trying to open a file that's already open.&lt;/p&gt;

&lt;p&gt;My question, then: How can I test whether the spreadsheet is already "being used by another program" (namely, Excel)? I can't do a filehandle test because I didn't open it in Perl.&lt;/p&gt;

&lt;p&gt;Respectfully awaiting enlightenment,&lt;br/&gt;
Your humble student Thnidu&lt;/p&gt;</field>
</data>
</node>
