The Joel on Software Discussion Group (CLOSED)

A place to discuss Joel on Software. Now closed.

This community works best when people use their real names. Please register for a free account.

Other Groups:
Joel on Software
Business of Software
Design of Software (CLOSED)
.NET Questions (CLOSED)
TechInterview.org
CityDesk
FogBugz
Fog Creek Copilot


The Old Forum


Your hosts:
Albert D. Kallal
Li-Fan Chen
Stephen Jones

How to check if a file is not already open ?

How can I check if a text file is not already open (by a 3rd party application, not mine)

Is there a dos command that does that, or an API ?

I'm aware that there's some sysinternals tool that does it but I really need a function that I can call from our corporate ERP system
Parisian Developer Send private email
Tuesday, April 10, 2007
 
 
if %open(filename);
  // do something;
endif;

You weren't languange specific...
flameThis
Tuesday, April 10, 2007
 
 
There is a utility available for UNIX-like operating systems called 'lsof' (list open files).

Will
Will Dowling Send private email
Tuesday, April 10, 2007
 
 
you need to try to open it *exclusively*, failing that someone else has it open.
onanon
Tuesday, April 10, 2007
 
 
"you need to try to open it *exclusively*, failing that someone else has it open."

That's the correct approach, but be sure to check the error code (again, OS/language specific) to be sure that's why it failed.

As a rule, the correct way to find out whether you can do X with a file is to try to do it and check the result. Testing first and then trying to do it introduces a race condition. The state of the file can change between testing it and doing something with it, in the OP's case between checking for another reader and opening the file.
clcr
Tuesday, April 10, 2007
 
 
And just keep in mind... not all programs lock a file when they open it.  Notepad doesn't, and many text editors don't.
Eric D. Burdo Send private email
Tuesday, April 10, 2007
 
 
lsof is Linux specific.

To the O/P, get the book "Advanced Windows Programming", which describes how these things can be looked up. Unfortunately, I don't know enough to give an answer off the top of my head.

Most common reason to want to know if a file is open is to be sure a file copy is complete. Is that what's going on here?
dot for this one
Tuesday, April 10, 2007
 
 
lemon obrien Send private email
Tuesday, April 10, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz