A modern, portable Fortran module for path manipulation and basic directory operations. This module provides a clean interface for working with file system paths in a platform-independent way. It correctly handles both Unix ('/') and Windows ('\') path separators through conditional compilation and offers deferred-length character results for maximum flexibility.
The module builds upon the fpx_string module for string type support and provides overloads of key procedures to accept either intrinsic character(*) or type(string) arguments.
Features
- Detection of absolute and rooted paths on Windows and Unix-like systems
- Safe path joining that avoids duplicate separators
- Extraction of directory part, filename (with or without extension)
- Path splitting into head/tail components
- Retrieval of the current working directory (cwd)
- Changing the current working directory (chdir)
- Note
- All path-returning functions return allocatable deferred-length characters.
-
The public generic join interface works with any combination of character and string.
character(:), allocatable :: p1, p2, full
p1 = '/home/user/docs'
p2 = 'report.pdf'
full = join(p1, p2)
print *, is_absolute(full)
print *, filename(full)
print *, filename(full,.true.)
print *, dirpath(full)
...
On Windows:
character(:), allocatable :: p
p = join('C:\Users', 'Alice', 'Documents')
print *, is_absolute(p)
...