Previous topic

Compiling and installing modFileSys

Next topic

List of routines

Table Of Contents

Using modFileSysΒΆ

In order to use modFileSys routines in your Fortran program, you only have to import the module libmodfiles_module into your code. Below you find a few examples demonstrating the usage of modFileSys. Some further examples can be found in the test/ folder within the source tree.

First let’s have an example demonstrating basic features (e.g. getting the name of the current directory, listing a directory, determining file sizes, canonizing path names, querying file sizes):

program test_ls
  use libmodfilesys_module

  type(dirdesc) :: dir
  character(:), allocatable :: path

  write(*, "(A)") "Current directory:"
  path = getcwd()
  write(*, "(A)") path
  write(*, "(A)") "Cannonized name of current directory:"
  write(*, "(A)") realpath(path)
  write(*, "(A)") "Listing current directory:"
  call opendir("./", dir)
  path = dir%next_filename()
  do while (len(path) > 0)
    write(*, "(A)") path
    write(*, "(A,I0)") "size: ", filesize(path)
    path = dir%next_filename()
  end do

end program test_ls

Here is something more advanced with creating and deleting directories recursivly plus manipulating symbolic links:

program test_mkdir_rmtree
  use libmodfilesys_module

  character(128) :: filename
  logical :: exists


  filename = "_testdir"
  exists = file_exists(filename)
  write(*, "(A,L2)") "Testing whether '_testdir' exists: ", exists
  if (exists) then
    write(*, "(A,L2)") "Is it a directory? ", isdir(filename)
    write(*, "(A,L2)") "Is it a symbolic link?", islink(filename)
    write(*, "(A)") "Hit [enter] to delete it recursively or Ctrl-C to stop"
    read(*, *)
    call rmdir(filename, children=.true.)
  else
    write(*, "(A)") "Creating directory '_testdir'"
    call mkdir("_testdir")
    write(*, "(A)") "Creating directory '_testdir/test2'"
    call mkdir("_testdir/test2")
    write(*, "(A)") "Creating file '_testdir/test2/file'"
    open(12, file="_testdir/test2/file")
    close(12)
    write(*, "(A)") "Creating a symlink to it as '_testdir/symlink'"
    call symlink("_testdir/test2/file", "_testdir/symlink")
    write(*, "(A,L2)") "Does `_testdir/symlink` exist?",&
        & islink("_testdir/symlink")
    write(*, "(A,A,A)") "Link shows to '", readlink("_testdir/symlink"), "'"
    write(*, "(A)") "Creating a hard ink to it as '_testdir/hardlink'"
    call link("_testdir/test2/file", "_testdir/hardlink")
    write(*, "(A)") "Creating recursive directory '_testdir/test3/test4/test5'"
    call mkdir("_testdir/test3/test4/test5", parents=.true.)
    write(*, "(A)") "Directories created, invoke the program again to&
        & delete them"
  end if
  write(*, "(A)") "Done."

end program test_mkdir_rmtree