forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Test for, and create, a directory

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Wed Jan 21, 2015 5:06 am    Post subject: Test for, and create, a directory Reply with quote

G'day, folks Smile

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
View user's profile Send private message Send e-mail Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7924
Location: Salford, UK

PostPosted: Wed Jan 21, 2015 9:05 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Wed Jan 21, 2015 9:09 am    Post subject: Reply with quote

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
View user's profile Send private message
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2388
Location: Yateley, Hants, UK

PostPosted: Wed Jan 21, 2015 3:42 pm    Post subject: Reply with quote

There are routines in the FTN77 library that may help.

Eddie
Back to top
View user's profile Send private message
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Thu Jan 22, 2015 3:45 am    Post subject: Re: Reply with quote

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 Smile

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 Smile

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Thu Jan 22, 2015 3:52 am    Post subject: Re: Reply with quote

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 Smile

That really is a clever approach Smile 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 Smile

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Thu Jan 22, 2015 3:55 am    Post subject: Re: Reply with quote

LitusSaxonicum wrote:
There are routines in the FTN77 library that may help.
Eddie


G'day, Eddie Smile

Time to refresh my memory on FTN77 library routines Smile

P.S. I still think that Clearwin is the best thing both before and since sliced bread Smile

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
LitusSaxonicum



Joined: 23 Aug 2005
Posts: 2388
Location: Yateley, Hants, UK

PostPosted: Thu Jan 22, 2015 11:13 am    Post subject: Reply with quote

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
View user's profile Send private message
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Thu Jan 22, 2015 11:45 am    Post subject: Re: Reply with quote

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 Smile

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
JohnCampbell



Joined: 16 Feb 2006
Posts: 2554
Location: Sydney

PostPosted: Thu Jan 22, 2015 11:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Fri Jan 23, 2015 7:40 pm    Post subject: Re: Reply with quote

JohnCampbell wrote:

... I have found files@ to be a useful and robust routine for these purposes.


Thankyou, John Smile

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
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group