Loading...
Searching...
No Matches
Path

Definition

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

Note
All path-returning functions return allocatable deferred-length characters.
The public generic join interface works with any combination of character and string.

Examples

character(:), allocatable :: p1, p2, full
p1 = '/home/user/docs'
p2 = 'report.pdf'
full = join(p1, p2) ! => '/home/user/docs/report.pdf'
print *, is_absolute(full) ! .true. (on Unix)
print *, filename(full) ! 'report'
print *, filename(full,.true.) ! 'report.pdf'
print *, dirpath(full) ! '/home/user/docs'
...

On Windows:

character(:), allocatable :: p
p = join('C:\Users', 'Alice', 'Documents')
! p == 'C:\Users\Alice\Documents'
print *, is_absolute(p) ! .true.
...

Data Types

interface  join
 Generic interface for joining two path components Supports all combinations of character and string arguments. More...
 

Methods

◆ chdir()

subroutine chdir ( character(*), intent(in) path,
integer, intent(out), optional err )

Changes the current working directory.

Parameters
[in]pathDirectory to change to
[out]errOptional 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]filepathPath 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]filepathPath 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]filepathFull or relative path
[in]keepextOptional; keep extension when .true.
Returns
res Filename (without path)
{.90}
print *, filename('dir/file.txt') ! 'file'
print *, filename('dir/file.txt',.true.) ! 'file.txt'
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
[in]filepathPath to test
Returns
res .true. if filepath is absolute
print *, is_absolute('/home/user') ! .true. (Unix)
print *, is_absolute('C:\\Temp') ! .true. (Windows)
print *, is_absolute('docs/..') ! .false.
...

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
[in]filepathPath to test
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]filepathInput path
[out]headDirectory part (includes trailing separator when appropriate)
[out]tailBase name part

Remarks

Definition at line 329 of file path.f90.