File name manipulation

Author(s): Daniel Cabeza, Angel Fernandez Pineda.

This library provides some small utilities to handle file name syntax.

Usage and interface

Documentation on exports

PREDICATE
This predicate will extract the last item (usually the file name) from a given path.

The first argument must be instantiated to a string or atom. Whenever the first argument is an atom, the second argument will be an atom. Whenever the first argument is a string, the second argument will be a string.

This predicate will fail under any of the following conditions:

  • First argument is not an atom, nor a string.
  • Second argument is not the last given path item (given path is the first argument).

Those are the most usual usages of no_path_file_name/2:

?- no_path_file_name("/home/nexusV/somefile.txt",K).

K = "somefile.txt" ? 

yes
?- no_path_file_name('/home/nexusV/somefile.txt',K).

K = 'somefile.txt' ? 

yes
?- 

Usage:no_path_file_name(Path,FileName)

FileName is the file corresponding to the given Path.

  • Call and exit should be compatible with:
    (filenames:atom_or_str/1)Path is an atom or a string
    (filenames:atom_or_str/1)FileName is an atom or a string

PREDICATE

Usage:file_directory_base_name(Path,Directory,BaseName)

Given a file path Path, Directory is the directory part and BaseName is the filename part. Directory does not end in '/' unless it is just '/'. Directory is '.' if Path does not contain '/'.

  • Call and exit should be compatible with:
    (filenames:atom_or_str/1)Path is an atom or a string
    (filenames:atom_or_str/1)Directory is an atom or a string
    (filenames:atom_or_str/1)BaseName is an atom or a string

PREDICATE
This predicate may be used in two ways:

  • To create a file name from its components: name and extension. For instance:

    ?- file_name_extension(File,mywork,'.txt').
    
    File = 'mywork.txt' ? 
    
    yes
    ?- 
    

  • To split a file name into its name and extension. For Instance:
    ?- file_name_extension('mywork.txt',A,B).
    
    A = mywork,
    B = '.txt' ? 
    
    yes
    ?- 
    

Any other usage of file_name_extension/3 will cause the predicate to fail. Notice that valid arguments are accepted both as atoms or strings.

Usage:file_name_extension(FileName,BaseName,Extension)

Splits a FileName into its BaseName and Extension.

  • Call and exit should be compatible with:
    (filenames:atom_or_str/1)FileName is an atom or a string
    (filenames:atom_or_str/1)BaseName is an atom or a string
    (filenames:atom_or_str/1)Extension is an atom or a string
General properties:

Test:file_name_extension(File,Name,Ext)

This is a bug, this test must succeeds.

  • If the following properties do not hold at call time:
    (term_basic:= /2)term_basic:File=/home/user/emacs.d/dummy
    then the following properties do not hold upon exit:
    (term_basic:= /2)term_basic:Name=/home/user/emacs.d/dummy
    (term_basic:= /2)term_basic:Ext=
    then the following properties do not hold globally:
    (native_props:is_det/1)All calls of the form file_name_extension(File,Name,Ext) are deterministic.
    (native_props:not_fails/1)All the calls of the form file_name_extension(File,Name,Ext) do not fail.

PREDICATE
basename(FileName,BaseName)

BaseName is FileName without extension. Equivalent to file_name_extension(FileName,BaseName,_). Useful to extract the base name of a file using functional syntax.

Usage:

  • Call and exit should be compatible with:
    (filenames:atom_or_str/1)FileName is an atom or a string
    (filenames:atom_or_str/1)BaseName is an atom or a string

REGTYPE

Usage:atom_or_str(X)

X is an atom or a string

    PREDICATE
    extension(FileName,Extension)

    Extension is the extension (suffix) of FileName. Equivalent to file_name_extension(FileName,_,Extension). Useful to extract the extension of a file using functional syntax.

    Usage:

    • Call and exit should be compatible with:
      (filenames:atom_or_str/1)FileName is an atom or a string
      (filenames:atom_or_str/1)Extension is an atom or a string