replace_filename Function

public function replace_filename(fname, newname) result(res)

Changes the filename of a path (keeping the path).

Syntax

 l = replace_filename(fname, newname)

Where:

  • fname: Name or path of the file.
  • newname: New name for the name filename (including extension).

It returns the fname value with the modified name.

Example

The following program changes the filename of some paths.

 PROGRAM replace_filenameExample
    USE FU_Files, ONLY: replace_filename
    IMPLICIT NONE
    CHARACTER(LEN=:), ALLOCATABLE :: path
    path='file1.txt'
    WRITE(*,*) path, ' ', replace_filename(path, 'file2')
    path='./file1.txt'
    WRITE(*,*) path, ' ', replace_filename(path, 'file2')
    path='/tmp/file1.txt'
    WRITE(*,*) path, ' ', replace_filename(path, 'file2')
    path='/tmp/file1.dat'
    WRITE(*,*) path, ' ', replace_filename(path, '')
    path='/tmp/file1'
    WRITE(*,*) path, ' ', replace_filename(path, 'file2')
 END PROGRAM replace_filenameExample

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: fname

Filename or path to a file.

character(len=*), intent(in) :: newname

New filename for the fname (including extension).

Return Value character(len=:), ALLOCATABLE

The fname value with the modified filename.


Contents