Silverfrost Forums

Welcome to our forums

Test for, and create, a directory

21 Jan 2015 4:06 #15364

**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

21 Jan 2015 8:05 #15365

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.

21 Jan 2015 8:09 #15366

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).

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
21 Jan 2015 2:42 #15374

There are routines in the FTN77 library that may help.

Eddie

22 Jan 2015 2:45 #15383

Quoted from PaulLaidler 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

22 Jan 2015 2:52 #15384

Quoted from davidb 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

22 Jan 2015 2:55 #15385

Quoted from LitusSaxonicum 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

22 Jan 2015 10:13 #15389

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

22 Jan 2015 10:45 #15390

Quoted from LitusSaxonicum

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

22 Jan 2015 10:09 #15401

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

23 Jan 2015 6:40 #15417

Quoted from JohnCampbell

... 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

Please login to reply.