View previous topic :: View next topic |
Author |
Message |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Aug 03, 2011 9:37 am Post subject: Testing for a directory |
|
|
The code below works well when testing for a valid directory using gfortran (on Linux). Trying the same code using FTN95 the inquire does not work to test for a directory.
Question: Does inquire only works for files? Which other possibilties exist to test for a valid directory using standard Fortran.
Code: |
program directory
implicit none
character(len=256) :: dir,sys_cmd
logical :: test_for_dir
dir = 'C:\temp\test_dir'
inquire(file=dir,exist=test_for_dir)
if (.not.test_for_dir) then
sys_cmd = 'mkdir '//TRIM(dir)
write(*,*) TRIM(sys_cmd)
call SYSTEM(sys_cmd)
else
write(*,*) 'Save files in '//TRIM(dir)
endif
end program directory |
|
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Wed Aug 03, 2011 10:46 am Post subject: |
|
|
I may be wrong but I thought that "CALL SYSTEM" was not standard fortran, with Silverfrost you can use either "CALL CISSUE@" or the function START_PROCESS@ both of which are also non-standard. _________________ John Horspool
Roshaz Software Ltd.
Gloucestershire |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Aug 03, 2011 10:55 am Post subject: |
|
|
The SYSTEM call is not to be found in the FTN95 documentation. However, it works, i.e. the directory is created - http://imageshack.us/photo/my-images/508/directory.jpg/
If the directory exists the inquire command does not work. Instead I should try to write to the directory. Then agian, the code works fine with gfortran. |
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Wed Aug 03, 2011 11:16 am Post subject: |
|
|
But isn't INQUIRE only supposed to be used for a unit number or a file? The FTN95 help makes no mention of using INQUIRE with directories and a brief search of the web only agreed with the FTN95 usage. I may be wrong again but it seems to me that gfortran might be using an extension to the standard in permitting directories to be inquired. _________________ John Horspool
Roshaz Software Ltd.
Gloucestershire |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Aug 03, 2011 11:44 am Post subject: |
|
|
I suppose that using a unit number is the correct way. This at least confirm what my understanding of inquire is.
I think the best solution would be to implement the directory test in some other way! The test is only performed at the beginning of the program, i.e. the effort would be manageable. |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Wed Aug 03, 2011 12:14 pm Post subject: |
|
|
May be this helps?
Code: |
WINAPP
OPTIONS(INTL)
PROGRAM TEST
IMPLICIT NONE
INCLUDE <WINDOWS.INS>
integer*2 dummy
character*200 pfad
pfad = 'c:\blabla\'
call attach@(pfad,dummy)
if (dummy .ne. 0) then
print*,'path did not exist'
call mkdir@(pfad,dummy)
if (dummy .eq. 0) print*,'path created'
else
print*,'path exists!'
end if
end |
Regards - Wilfried |
|
Back to top |
|
 |
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2415 Location: Yateley, Hants, UK
|
Posted: Wed Aug 03, 2011 1:51 pm Post subject: |
|
|
Wilfried, a clever solution. This whole post is complemented by the discussion over FEXISTS@ in this forum just a few threads back.
Eddie |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Aug 03, 2011 2:15 pm Post subject: |
|
|
Thanks for the example and the tipp!
Both solutions make use of FTN95 non-standard solutions. And the aim is to avoid this. I found several entries where the inquire function works for testing directories as well (link):
Quote: |
> > > > > INQUIRE(FILE='some directory specification',EXIST=ex) correctly tests
> > > > > for the existence of a directory with all my Windows compilers
> > > > > (g95,gfortran,lf95) except IVF. Does anybody know the reason for this
> > > > > and what the standard says if anything?
|
This topic is also discussed in other Fortran forums and it seems like the Fortran standard does not provide anything in this regard. However, according to http://rosettacode.org/wiki/Ensure_that_a_file_exists#Fortran it shoul be possible. |
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Aug 03, 2011 2:41 pm Post subject: |
|
|
The following code is portable provided the directory is not on a read only device like a CD-ROM.
Basic approach works in F77 too but without the module wrapper obviously.
Code: |
module file_utils
contains
function is_dir_exists(name)
character (len=*), intent(in) :: name
logical is_dir_exists
integer :: icode
! If the file'abcde' is in the directory, the directory exists
inquire(file=trim(name)//'/abcde', exist=is_dir_exists)
! That's unlikely so use another strategy
if (.not. is_dir_exists) then
! If I can create file 'abcde' in the directory, the directory exists
open(99,file=trim(name)//'/abcde',iostat=icode)
! Directory exists if open was successful, otherwise it doesn't
is_dir_exists = (icode == 0)
! Delete the file
if (icode == 0) close(99,status='delete')
end if
end function
end module file_utils
program anon
use file_utils
if (is_dir_exists("tmp")) then
print *, "Directory exists"
else
print *, "Directory does not exist"
end if
end program anon
|
_________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Last edited by davidb on Wed Aug 03, 2011 2:59 pm; edited 1 time in total |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Aug 03, 2011 2:55 pm Post subject: |
|
|
This works excellent! Thanks.
Even though the call to system is not documented it works. The following code is the proof:
Code: |
program anon
use file_utils
character(len=256) :: dir,sys_cmd
dir = 'c:\temp\test1'
if (is_dir_exists(dir)) then
print *, "Directory exists"
else
print *, "Directory does not exist"
write(*,*) 'Create the new directory '//trim(dir)
sys_cmd = 'mkdir '//trim(dir)
call system(sys_cmd)
end if
end program anon |
Paul, could you make a comment on the call to system. I could not find it in the documentation. However, it works for both FTN95 and gfortran. |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Aug 03, 2011 3:00 pm Post subject: |
|
|
You might want to note I added a condition to the close line. Its possible that close on an unopened file is just ignored so its ok without this additional check; I don't have time to check this now.
Do you want the directory creation to be portable too? _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Wed Aug 03, 2011 3:08 pm Post subject: |
|
|
Is it possible that the result of inquire has to do with evironment variables or something like that? If you like to test the following code, please note that "c:\blabla" doesn't exist on my PC, but "c:\homepage" does (so, please replace this path by one that is existing on your PC):
Code: |
WINAPP
OPTIONS(INTL)
PROGRAM TEST
IMPLICIT NONE
INCLUDE <WINDOWS.INS>
logical*2 L
print*,'c:\blabla (not existing)'
inquire(file='c:\blabla\',exist=L)
print*,L
print*,'c:\program files (x86) (existing)'
inquire(file='c:\program files (x86)\',exist=L)
print*,L
print*,'c:\homepage (existing)'
inquire(file='c:\homepage\',exist=L)
print*,L
print*,'c:\temp (existing)'
inquire(file='c:\temp\',exist=L)
print*,L
end |
|
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Aug 03, 2011 3:11 pm Post subject: |
|
|
The directory creation must be portable as well. Are there other options beside a call to system?
The FEM code was initially to be used on a windows system. In the meanwhile we found that the performance and developing to be easier on Linux - at least for our application. |
|
Back to top |
|
 |
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Aug 03, 2011 3:35 pm Post subject: Re: |
|
|
jjgermis wrote: |
Are there other options beside a call to system?
|
No, sadly. Maybe there's something in Fortran 2008 for the future. _________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|
Back to top |
|
 |
|