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)
...
|
| interface | join |
| | Generic interface for joining two path components Supports all combinations of character and string arguments. More...
|
| |
◆ chdir()
| subroutine chdir |
( |
character(*), intent(in) | path, |
|
|
integer, intent(out), optional | err ) |
Changes the current working directory.
- Parameters
-
| [in] | path | Directory to change to |
| [out] | err | Optional integer error code (0 = success, non-zero = failure) integer :: ierr
call chdir('/tmp', ierr)
if (ierr /= 0) stop 'Failed to change directory'
|
Remarks
Definition at line 422 of file path.f90.
◆ cwd()
| character(:) function, allocatable cwd |
Returns the current working directory as a deferred-length character string. Returns empty string on failure.
- Returns
- res Current working directory
character(:), allocatable :: here
here = cwd()
print *, 'We are in: ', here
Remarks
Definition at line 392 of file path.f90.
◆ dirname()
| pure character(:) function, allocatable dirname |
( |
character(*), intent(in) | filepath | ) |
|
Returns the base name (filename) part of a path.
- Parameters
-
| [in] | filepath | Path to analyse |
- Returns
- res Base name component
print *, dirname('/home/user/file.txt') ! 'file.txt'
Remarks
Definition at line 313 of file path.f90.
◆ dirpath()
| pure character(:) function, allocatable dirpath |
( |
character(*), intent(in) | filepath | ) |
|
Returns the directory part of a path (everything before the last separator).
- Parameters
-
| [in] | filepath | Path to analyse |
- Returns
- res Directory component
print *, dirpath('/home/user/file.txt') ! '/home/user'
Remarks
Definition at line 294 of file path.f90.
◆ filename()
| pure character(:) function, allocatable filename |
( |
character(*), intent(in) | filepath, |
|
|
logical, intent(in), optional | keepext ) |
Extracts the filename part of a path. By default the extension is stripped. If keepext=.true. the full filename including extension is returned.
- Parameters
-
| [in] | filepath | Full or relative path |
| [in] | keepext | Optional; keep extension when .true. |
- Returns
- res Filename (without path)
{.90}
print *, filename('dir/file.txt')
print *, filename('dir/file.txt',.true.)
print *, filename('archive.tar.gz') ! 'archive.tar'
Remarks
Definition at line 179 of file path.f90.
◆ is_absolute()
| pure logical function is_absolute |
( |
character(*), intent(in) | filepath | ) |
|
Returns .true. if the path is absolute. On Unix a path is absolute when it starts with '/'. On Windows a path is absolute when it starts with a drive letter followed by ':\C:\', 'd:/temp').
- Parameters
-
- Returns
- res .true. if filepath is absolute
print *, is_absolute('/home/user')
print *, is_absolute('C:\\Temp')
print *, is_absolute('docs/..')
...
Remarks
Definition at line 127 of file path.f90.
◆ is_rooted()
| pure logical function is_rooted |
( |
character(*), intent(in) | filepath | ) |
|
Returns .true. if the path is rooted (starts with a separator) or is absolute. A rooted path begins with the platform separator ('\' on Windows, '/' elsewhere) even if it is not a full absolute path (e.g. '/temp' on Linux).
- Parameters
-
- Returns
- res .true. if filepath is rooted
Remarks
Definition at line 150 of file path.f90.
◆ split_path()
| pure subroutine split_path |
( |
character(*), intent(in) | filepath, |
|
|
character(:), intent(out), allocatable | head, |
|
|
character(:), intent(out), allocatable | tail ) |
Splits a path into head (directory) and tail (basename) components.
- Parameters
-
| [in] | filepath | Input path |
| [out] | head | Directory part (includes trailing separator when appropriate) |
| [out] | tail | Base name part |
Remarks
Definition at line 329 of file path.f90.