I don't know how you open the file but it should be possible to open the
file using a sharing mode that causes the open to fail with a meaningful
error code if another process has it open. You should be using a relevant
sharing code and checking the error codes from open anyway. However it is
also a good idea to wait on the other program as you are asking about.
For scripting purposes, WMI can be very useful and powerful. You can use WMI
to monitor the other process. The following is not about just WMI, but WMI
is used so much that it is very relevant.
http://www.microsoft.com/technet/community/scriptcenter/default.mspx
Two things in particular to look at is the "Download all the scripts in HTML
Help format" and the "Scriptomatic". The scripts that can be downloaded are
also available for online viewing. The following are more specific places to
look for some relevant samples.
http://www.microsoft.com/technet/community/scriptcenter/process/default.mspx
http://www.microsoft.com/technet/community/scriptcenter/monitor/default.mspx
If you can't find a more appropriate solution using WMI, you can probably do
what you need to do by monitoring process deletion.
WMI is quite easy to use in scripts, and once you are familiar with it,
there is a lot that you can do.