Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Existence of a file

1 view
Skip to first unread message

Gib Bogle

unread,
Mar 10, 2010, 10:29:06 PM3/10/10
to
I want to check for the existence of a file, in order to determine whether the
coast is clear to create this file. There is another process that opens the
file, reads it, then removes it. The plan is to wait (sleep) until the file has
been processed and deleted. INQUIRE(filename,EXIST=ex) doesn't seem to fit the
bill, since this will return ex = .false. if the other process currently has the
file open. What's the best way to do what I want?

Richard Maine

unread,
Mar 10, 2010, 11:51:40 PM3/10/10
to
Gib Bogle <g.b...@auckland.no.spam.ac.nz> wrote:

I have no idea why you are getting a false from that just because some
other process has the file open. That makes little sense to me. Seems
like that ought to be an answer and that if this is accurately reported,
it would be a bug. A file doesn't cease to exist just because it is
open.

However...

Using an OPEN with status='new' will get you an error on many systems if
the file already exists. A caveat though is that on systems with file
versions (VAX VMS), it wil just create a new version.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain

Sjouke Burry

unread,
Mar 10, 2010, 11:58:53 PM3/10/10
to

something like call system("dir [path_and_filename] > dirlist.txt")

and reading the result in dirlist.txt?

Then waiting a bit when the file is still around and repeat test?

Choose the commands according to your OS.

Gib Bogle

unread,
Mar 11, 2010, 12:30:58 AM3/11/10
to
Richard Maine wrote:
> Gib Bogle <g.b...@auckland.no.spam.ac.nz> wrote:
>
>> I want to check for the existence of a file, in order to determine whether
>> the coast is clear to create this file. There is another process that
>> opens the file, reads it, then removes it. The plan is to wait (sleep)
>> until the file has been processed and deleted. INQUIRE(filename,EXIST=ex)
>> doesn't seem to fit the bill, since this will return ex = .false. if the
>> other process currently has the file open. What's the best way to do what
>> I want?
>
> I have no idea why you are getting a false from that just because some
> other process has the file open. That makes little sense to me. Seems
> like that ought to be an answer and that if this is accurately reported,
> it would be a bug. A file doesn't cease to exist just because it is
> open.

I'm not getting a false, I haven't coded this yet. Maybe I misinterpreted the
Intel help info:

ex
Is a scalar default logical variable that is assigned one of the following
values:

.TRUE.
If the specified file exists and can be opened, or if the specified unit exists

.FALSE.
If the specified file or unit does not exist or if the file exists but cannot
be opened

I was assuming that the file couldn't be opened if the other process had it open.

>
> However...
>
> Using an OPEN with status='new' will get you an error on many systems if
> the file already exists. A caveat though is that on systems with file
> versions (VAX VMS), it wil just create a new version.
>

I don't think I have to worry about VAXen.

Gib Bogle

unread,
Mar 11, 2010, 12:31:38 AM3/11/10
to

That's certainly feasible, but a bit complicated.

glen herrmannsfeldt

unread,
Mar 11, 2010, 1:42:18 AM3/11/10
to
Gib Bogle <g.b...@auckland.no.spam.ac.nz> wrote:
> Richard Maine wrote:
>> Gib Bogle <g.b...@auckland.no.spam.ac.nz> wrote:

>>> I want to check for the existence of a file, in order to determine whether
>>> the coast is clear to create this file. There is another process that
>>> opens the file, reads it, then removes it. The plan is to wait (sleep)
>>> until the file has been processed and deleted. INQUIRE(filename,EXIST=ex)
>>> doesn't seem to fit the bill, since this will return ex = .false. if the
>>> other process currently has the file open. What's the best way to do what
>>> I want?

>> I have no idea why you are getting a false from that just because some
>> other process has the file open. That makes little sense to me. Seems
>> like that ought to be an answer and that if this is accurately reported,
>> it would be a bug. A file doesn't cease to exist just because it is
>> open.

It sounds like what Windows might do...



> I'm not getting a false, I haven't coded this yet.
> Maybe I misinterpreted the

You might check to see what it really does, but I agree that
sounds like what it might do. My best understanding is that other
programs can open the file for read, but not read/write, even if
the program doesn't actually plan to write to the file.

> Intel help info:

> ex
> Is a scalar default logical variable that is assigned one of
> the following values:

> .TRUE.
> If the specified file exists and can be opened, or if the specified
> unit exists

> .FALSE.
> If the specified file or unit does not exist or if the file
> exists but cannot be opened

> I was assuming that the file couldn't be opened if the other
> process had it open.

If you find the right OPEN parameters, such that it will create
a new file if it doesn't exist, won't overwrite the old one,
and will appropriately fail if someone else had it open for reading,
then you can do a sleep/open loop until it succeeds. Otherwise,
it seems that the problem is Windows specific and you need
Windows API calls to find out about the file.



>> However...

>> Using an OPEN with status='new' will get you an error on many systems if
>> the file already exists. A caveat though is that on systems with file
>> versions (VAX VMS), it wil just create a new version.

> I don't think I have to worry about VAXen.

VMS is still around, with new versions only for Alpha and Itanium.

-- glen

Arjen Markus

unread,
Mar 11, 2010, 4:56:02 AM3/11/10
to

If INQUIRE does behave that way, perhaps:

OPEN( 10, FILE = filename, STATUS = 'OLD', ACTION = 'READ', IOSTAT =
ierr )

will help?

ierr should be unequal zero, if the file exists and can be read (you
should
be able to read it even if another process has opened it). Possibly
another
check (try open the file as new) is required to be fully certain it
does
not exist and the first check did not fail because the file could not
be
opened for reading only - this would solve the situation of versioned
files.

Regards,

Arjen

Paul van Delst

unread,
Mar 11, 2010, 10:26:18 AM3/11/10
to

It's been my experience that many processes can open and use the same file (in different
spots) at the same time. Although, that was on a IBM SP. Maybe "can be opened" means the
user in question has read and/or write permission for the file?

FWIW, the following snippet from my "File_Utility" module shows the functions I use (since
2000/2001) to test for existence and open-ness. I've never had a problem with them on any
machine I've used (yet, at least :o)


INTERFACE File_Exists
MODULE PROCEDURE File_Unit_Exists
MODULE PROCEDURE File_Name_Exists
END INTERFACE File_Exists

INTERFACE File_Open
MODULE PROCEDURE File_Open_by_Unit
MODULE PROCEDURE File_Open_by_Name
END INTERFACE File_Open

CONTAINS

FUNCTION File_Unit_Exists( FileID ) RESULT( Existence )
INTEGER, INTENT(IN) :: FileID
LOGICAL :: Existence
INQUIRE( UNIT = FileID, EXIST = Existence )
END FUNCTION File_Unit_Exists

FUNCTION File_Name_Exists( Filename ) RESULT( Existence )
CHARACTER(*), INTENT(IN) :: Filename
LOGICAL :: Existence
INQUIRE( FILE = Filename, EXIST = Existence )
END FUNCTION File_Name_Exists


FUNCTION File_Open_by_Unit( FileID ) RESULT( Is_Open )
INTEGER, INTENT(IN) :: FileID
LOGICAL :: Is_Open
INQUIRE( UNIT = FileID, OPENED = Is_Open )
END FUNCTION File_Open_by_Unit

FUNCTION File_Open_by_Name( Filename ) RESULT( Is_Open )
CHARACTER(*), INTENT(IN) :: Filename
LOGICAL :: Is_Open
INQUIRE( FILE = Filename, OPENED = Is_Open )
END FUNCTION File_Open_by_Name


cheers,

paulv

Gib Bogle

unread,
Mar 11, 2010, 7:30:24 PM3/11/10
to
Paul van Delst wrote:
>
> It's been my experience that many processes can open and use the same file (in different
> spots) at the same time. Although, that was on a IBM SP. Maybe "can be opened" means the
> user in question has read and/or write permission for the file?
>
> FWIW, the following snippet from my "File_Utility" module shows the functions I use (since
> 2000/2001) to test for existence and open-ness. I've never had a problem with them on any
> machine I've used (yet, at least :o)
>
>
> INTERFACE File_Exists
> MODULE PROCEDURE File_Unit_Exists
> MODULE PROCEDURE File_Name_Exists
> END INTERFACE File_Exists
>
etc.

I'm going to start with this approach, and stick with it until I have a problem.

Gib Bogle

unread,
Mar 11, 2010, 7:31:10 PM3/11/10
to
Arjen Markus wrote:
>
> If INQUIRE does behave that way, perhaps:
>
> OPEN( 10, FILE = filename, STATUS = 'OLD', ACTION = 'READ', IOSTAT =
> ierr )
>
> will help?
>
> ierr should be unequal zero, if the file exists and can be read (you
> should
> be able to read it even if another process has opened it). Possibly
> another
> check (try open the file as new) is required to be fully certain it
> does
> not exist and the first check did not fail because the file could not
> be
> opened for reading only - this would solve the situation of versioned
> files.
>
> Regards,
>
> Arjen

I'll resort to this if the simple INQUIRE(EXIST..) fails.

0 new messages