- Reference manual
- SWI-Prolog C-library
- library(process): Create processes and redirect I/O
- library(filesex): Extended operations on files
- library(uid): User and group management on Unix systems
- library(syslog): Unix syslog interface
- library(socket): Network socket (TCP and UDP) library
- The stream_pool library
- library(uri): Process URIs
- CGI Support library
- Password encryption library
- library(uuid): Universally Unique Identifier (UUID) Library
- SHA* Secure Hash Algorithms
- library(md5): MD5 hashes
- library(hash_stream): Maintain a hash on a stream
- Memory files
- Time and alarm library
- library(unix): Unix specific operations
- Limiting process resources
- library(udp_broadcast): A UDP broadcast proxy
- library(prolog_stream): A stream with Prolog callbacks
- SWI-Prolog C-library
This module provides additional operations on files. This covers both more obscure and possible non-portable low-level operations and high-level utilities.
Using these Prolog primitives is typically to be preferred over using operating system primitives through shell/1 or process_create/3 because (1) there are no potential file name quoting issues, (2) there is no dependency on operating system commands and (3) using the implementations from this library is usually faster.
- [det]set_time_file(+File, -OldTimes, +NewTimes)
- Query and set POSIX time attributes of a file. Both OldTimes
NewTimes are lists of option-terms. Times are represented in
SWI-Prolog's standard floating point numbers. New times may be specified
nowto indicate the current time. Defined options are:
- Describes the time of last access of the file. This value can be read and written.
- Describes the time the contents of the file was last modified. This value can be read and written.
- Describes the time the file-structure itself was changed by adding (
link()) or removing (
Below are some example queries. The first retrieves the access-time, while the second sets the last-modified time to the current time.
?- set_time_file(foo, [access(Access)], ). ?- set_time_file(foo, , [modified(now)]).
- [det]link_file(+OldPath, +NewPath, +Type)
- Create a link in the filesystem from NewPath to OldPath. Type
defines the type of link and is one of
With some limitations, these functions also work on Windows. First of all, the underlying filesystem must support links. This requires NTFS. Second, symbolic links are only supported in Vista and later.
domain_error(link_type, Type)if the requested link-type is unknown or not supported on the target OS.
- [det]relative_file_name(+Path:atom, +RelToFile:atom, -RelPath:atom)
- [det]relative_file_name(-Path:atom, +RelToFile:atom, +RelPath:atom)
- True when RelPath is Path, relative to the file RelToFile. Path
and RelTo are first handed to absolute_file_name/2,
which makes the absolute and canonical. Below are two examples:
?- relative_file_name('/home/janw/nice', '/home/janw/deep/dir/file', Path). Path = '../../nice'. ?- relative_file_name(Path, '/home/janw/deep/dir/file', '../../nice'). Path = '/home/janw/nice'.
Add a terminating
/to get a path relative to a directory, e.g.
?- relative_file_name('/home/janw/deep/dir/file', './', Path). Path = 'deep/dir/file'.
All paths must be in canonical POSIX notation, i.e., using / to separate segments in the path. See prolog_to_os_filename/2.
- It would probably have been cleaner to use a directory as second argument. We can not do such dynamically as this predicate is defined as a syntactical operation, which implies it may be used for non-existing paths and URLs.
- [det]directory_file_path(+Directory, +File, -Path)
- [det]directory_file_path(?Directory, ?File, +Path)
- True when Path is the full path-name for File in
Dir. This is comparable to
atom_concat(Directory, File, Path), but it ensures there is exactly one / between the two parts. Notes:
- [nondet]directory_member(+Directory, -Member, +Options)
- True when Member is a path inside Directory. Options
false), recurse into subdirectories
true(default), follow symbolic links.
- See absolute_file_name/3.
- Only return entries whose extension appears in List.
- How to handle errors. One of
error. Default is
warning. Errors notably happen if a directory is unreadable or a link points nowhere.
- Only return entries with Access
- Only return files that match GlobPattern.
- Exclude files matching GlobPattern.
- Do not recurse into directories matching GlobPattern.
true(default), also return hidden files.
This predicate is safe against cycles introduced by symbolic links to directories.
The idea for a non-deterministic file search predicate comes from Nicos Angelopoulos.
- [det]copy_file(+From, +To)
- Copy a file into a new file or directory. The data is copied as binary data.
- Create Dir and all required components (like mkdir -p). Can raise various file-specific exceptions.
- [det]copy_directory(+From, +To)
- Copy the contents of the directory From to To (recursively). If To is the name of an existing directory, the contents of From are copied into To. I.e., no subdirectory using the basename of From is created.
- Recursively remove the directory Dir and its contents. If Dir is a symbolic link or symbolic links inside Dir are encountered, the links are removed rather than their content. Use with care!
- Remove all content from directory Dir, without removing Dir itself. Similar to delete_directory_and_contents/2, if symbolic links are encountered in Dir, the links are removed rather than their content.
- [det]chmod(+File, +Spec)
- Set the mode of the target file. Spec is one of
-Modeor a plain Mode, which adds new permissions, revokes permissions or sets the exact permissions. Mode itself is an integer, a POSIX mode name or a list of POSIX mode names. Defines names are
svtxand all names defined by the regular expression
[ugo]*[rwx]*. Specifying none of "ugo" is the same as specifying all of them. For example, to make a file executable for the owner (user) and group, we can use:
?- chmod(myfile, +ugx).