View previous topic :: View next topic |
Author |
Message |
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Wed Jan 21, 2015 5:06 am Post subject: Test for, and create, a directory |
|
|
G'day, folks
Within a Fortran/Clearwin program is it possible to test for the existence of a sub-directory (folder) and, if it does not exist within the current directory, to create it?
Eric, in Perth, Western Australia |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7924 Location: Salford, UK
|
Posted: Wed Jan 21, 2015 9:05 am Post subject: |
|
|
Try calling ATTACH@ but make sure that you use the right KIND for the returned error code. See ftn95.chm for details. Call MKDIR@ and not ATTACH@ if you want to create the folder anyway. |
|
Back to top |
|
|
davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Wed Jan 21, 2015 9:09 am Post subject: |
|
|
The code below tests for the presence of a directory and uses only standard Fortran. It will not work if the device is not writable (e.g. a CD Rom).
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 |
|
Back to top |
|
|
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2388 Location: Yateley, Hants, UK
|
Posted: Wed Jan 21, 2015 3:42 pm Post subject: |
|
|
There are routines in the FTN77 library that may help.
Eddie |
|
Back to top |
|
|
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Thu Jan 22, 2015 3:45 am Post subject: Re: |
|
|
PaulLaidler wrote: | Try calling ATTACH@ but make sure that you use the right KIND for the returned error code. See ftn95.chm for details. Call MKDIR@ and not ATTACH@ if you want to create the folder anyway. |
Thankyou, Paul
A bit of background ...
I've written and successfully compiled a program to either modify or create a pedigree chart for use by a dog-breeding friend. The program requires the source text files to be in a sub-directory \pedigree_files - which could be created anywhere on my friend's computer.
Problem is, to my limited? understanding, that ATTACH@ requires the full path specification, beginning with a device name such as C:\ ATTACH@ returns an error code if I specify the path as simply \pedigree_files
I've just thought of a possible way around that restriction ... rather than the program trying to open, say, \pedigree_files\fn301.txt (where fn301.txt contains the pedigree data) the program could try opening just fn301.txt (which assumes that fn301.txt and the .exe program are in the same location)
Thanks again for the suggestion; it got me thinking
Eric |
|
Back to top |
|
|
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Thu Jan 22, 2015 3:52 am Post subject: Re: |
|
|
davidb wrote: | The code below tests for the presence of a directory and uses only standard Fortran. It will not work if the device is not writable (e.g. a CD Rom). |
Thankyou, davidb
That really is a clever approach I think I came up with something similar in my reply to Paul - if one thing doesn't work try something else - a search rather than a direct straight-in approach to the destination
Eric |
|
Back to top |
|
|
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Thu Jan 22, 2015 3:55 am Post subject: Re: |
|
|
LitusSaxonicum wrote: | There are routines in the FTN77 library that may help.
Eddie |
G'day, Eddie
Time to refresh my memory on FTN77 library routines
P.S. I still think that Clearwin is the best thing both before and since sliced bread
Eric |
|
Back to top |
|
|
LitusSaxonicum
Joined: 23 Aug 2005 Posts: 2388 Location: Yateley, Hants, UK
|
Posted: Thu Jan 22, 2015 11:13 am Post subject: |
|
|
Eric,
If the data file sub directory is related to the program files directory in some obvious sort of way, e.g.
C:\program files\doggie\code
and
c:\program files\doggie\data
then you can use GET_PROGRAM_NAME@ to find the path to your EXE, then trim off the end bits and add the bits you want. (i.e. trim off code\woof.exe from
C:\program files\doggie\code\woof.exe
and add back data\info.dat to get
C:\program files\doggie\data\info.dat
Or, you can store the path to the data file in an INI file (old fashioned, but needs no knowledge of the registry), and get the location of the INI file in the same way.
Eddie |
|
Back to top |
|
|
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Thu Jan 22, 2015 11:45 am Post subject: Re: |
|
|
LitusSaxonicum wrote: |
If the data file sub directory is related to the program files directory in some obvious sort of way, ... |
That's several new things - well, new to me, anyway.
Thankyou, Eddie
Eric |
|
Back to top |
|
|
JohnCampbell
Joined: 16 Feb 2006 Posts: 2554 Location: Sydney
|
Posted: Thu Jan 22, 2015 11:09 pm Post subject: |
|
|
You may have some success using files@ for the directory containing the sub directory. This will return the full tree name and also the type of directory entry that it is.
I have found files@ to be a useful and robust routine for these purposes.
John |
|
Back to top |
|
|
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Fri Jan 23, 2015 7:40 pm Post subject: Re: |
|
|
JohnCampbell wrote: |
... I have found files@ to be a useful and robust routine for these purposes.
|
Thankyou, John
I think I will be sharing your experience of the usefulness; files@ is a very comprehensive routine.
http://www.silverfrost.com/ftn95-help/file_man/h_filesa.aspx
Eric |
|
Back to top |
|
|
|