Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to know when in process file is closed by another process.

Status
Not open for further replies.

shagymoe

Programmer
Apr 20, 2001
72
US
Hi all,

We have an application which writes temporary files to a directory. These files are typically open and being written to by that application for about 3 seconds and then they are deleted and another temp file is opened and written with the same filename. They are deleted because as soon as the file is complete it prints (It is a zebra printer file).

I am trying to write a script which will capture all the information in the file before it is deleted by the application. The reason I want to do this is so that I can reprint it if necessary. The file is written by a database and the application that tries to do reprints often takes 2.5 minutes to reprint one label. This is a big problem if you have to reprint 100 labels!!! The database people say that they have done all they can to optimize the reprint time and the application people won't change it to save the file anywhere else permanently so I gotta hack it myself. If I can capture this label file before it prints, then I will have a permanent copy which I can print at any time and I could select 100 of them and reprint them all in 1 second!!!

The problem is knowing when the file is complete. I have written this snippet of code to find out if the eof() function would work, but it is fooled into thinking that the end of file is just the most recent line written to the file.


while (1) {

$dir="c:\labels";

opendir DIR, $dir or die "Can't open $dir: $!\n";

while ($file = readdir DIR) {

$in_file_path="$dir\\$file";

if ($file=~/^tmp/ig) {

open IN, "$in_file_path" or die "Can't open $in_file_path: $!\n";

while (<IN>) {

if (eof) {

print &quot;End of file is $. $_\n&quot;;
}
}
close IN;
}
}
}

The results show the file building like so:

&quot;End of file is 60 $_stuff&quot;
&quot;End of file is 63 $_stuff&quot;
&quot;End of file is 67 $_stuff&quot;
&quot;End of file is 75 $_stuff&quot;
&quot;End of file is 77 $_stuff&quot;
&quot;End of file is 78 $_stuff&quot;

So you can see that eof() doen't work and there is nothing written to the end of the file to let you know that the end has been reached. The zebra language has end of label syntax, but this is used more than once in the label file.

These files are always less than 6k and sometimes only 2k, so they don't stick around long. I tried to find a function which will tell me if the file is open by another application, but I've had no luck. Anyone here have any ideas?

Thanks a million. I'm just a beginning perl hacker, so excuse me if this is simple.

Shagy
 
If you were on a Unix-like system, I'd have a solution for you, but I'm pretty sure windows systems don't support hard links, and I don't know enough about windows file handling to give you any ideas.

Sorry,
Richard
 
I've found a little (69k) app which queries the OS to find out what process has what file open.

You can find it here:
______________________________________________________________________
Did you know?
The quality of our answers is directly proportional to the number of stars you vote?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top