Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test whether a directory exists or not

I'm trying to verify that a directory exists using Fortan90. On various sites I found:

logical :: dir_e
inquire(file='./docs/.', exist=dir_e)

if ( dir_e ) then
  write(*,*) "dir exists!"
else
  ! workaround: it calls an extern program...
  call system('mkdir docs')
end if

However, inquire returns False whether or not the directory exists and if I execute this code twice, I get an error message

cannot make dir, file already exists

If I use:

inquire(file='./docs/test', exist=dir_e)

with an existing file test, inquire returns true.

How can I check for the existence of a directory? I am using ubuntu 11.04 and the ifort compiler.

like image 636
Sebastian Avatar asked Mar 01 '12 19:03

Sebastian


2 Answers

The Fortran standard 95, 2003 and 2008 do not specify, how inquire should treat directories. From my experience under Linux, gfortran treats them as files, ifort does not. The directory statement is a proprietary feature of ifort and should therefore be avoided.

The safest would be to test for a file in the said directory.

like image 161
Mison Avatar answered Sep 21 '22 17:09

Mison


The following should work:

INQUIRE (DIRECTORY=dir, EXIST=ex [, DIRSPEC=dirspec] [, ERR=label] [, IOSTAT=i-var] )

I don't have ifort on this machine so I can't test it.

Addendum: The code posted originally works with gfortran. The DIRECTORY statement works with ifort but not with gfortran.

And in case for more information check: http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/win/compiler_f/lref_for/source_files/rfinquir.htm#rfinquir

like image 24
Azrael3000 Avatar answered Sep 17 '22 17:09

Azrael3000