basename(3f) - [M_io:PATHNAMES] return last component from filename (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
See Also
Author
License
function basename(FILENAME,SUFFIX) result (LEAF)
character(len=:),allocatable :: FILENAME character(len=*),intent(in),optional :: SUFFIX character(len=*),intent(in) :: LEAF
Output LEAF of filename with directory paths removed.Assumes leaf separator is a slash or backslash as determined by separator(3f) and that filename does not contain trailing spaces.
FILENAME pathname to extract the last leaf from SUFFIX suffix to remove. If not present the rightmost ".string" string is removed. If present the LEAF is returned with any matching suffix removed.
LEAF returned leaf name
Sample program:
program demo_basename use M_io, only : basename implicit none character(len=:),allocatable :: fn integer :: filename_length integer :: i ! get pathname from command line arguments do i = 1, command_argument_count() call get_command_argument (i, length=filename_length) if(allocated(fn))deallocate(fn) allocate(character(len=filename_length) :: fn) call get_command_argument (i, value=fn) ! leaf with any suffix removed ! leaf with suffix retained ! with suffix unless it is ".f90" write(*,(*(a,1x))) basename(fn), basename(fn,), basename(fn,.f90) enddo end program demo_basenameSample program executions:
$demo_basename /usr/bin/ bin bin bin $demo_basename dir1/fred.x dir2/.y fred fred.x fred.x .y .y .y $demo_basename stdio.h stdio stdio.h stdio.h $demo_basename /name.f90 name name.f90 name
basename(3c), basename(3c), readlink(3c), realpath(3c)
John S. Urban
Public Domain
Nemo Release 3.1 | basename (3) | February 23, 2025 |