View source code
Display the source code in std/file.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Module std.file

Utilities for manipulating files and scanning directories. Functions in this module handle files as a unit, e.g., read or write one file at a time. For opening files and manipulating them via handles refer to module std.stdio.

See Also

The official tutorial for an introduction to working with files in D, module std.stdio for opening files and manipulating them via handles, and module std.path for manipulating path strings.

Functions

NameDescription
append(name, buffer) Appends buffer to file name.
attrIsDir(attributes) Returns whether the given file attributes are for a directory.
attrIsFile(attributes) Returns whether the given file attributes are for a file.
attrIsSymlink(attributes) Returns whether the given file attributes are for a symbolic link.
chdir(pathname) Change directory to pathname. Equivalent to cd on Windows and POSIX.
copy(from, to, preserve) Copy file from to file to. File timestamps are preserved. File attributes are preserved, if preserve equals Yes.preserveAttributes. On Windows only Yes.preserveAttributes (the default on Windows) is supported. If the target file exists, it is overwritten.
dirEntries(path, mode, followSymlink) Returns an input range of DirEntry that lazily iterates a given directory, also provides two ways of foreach iteration. The iteration variable can be of type string if only the name is needed, or DirEntry if additional details are needed. The span mode dictates how the directory is traversed. The name of each iterated directory entry contains the absolute or relative path (depending on pathname).
exists(name) Determine whether the given file (or directory) exists.
getAttributes(name) Returns the attributes of the given file.
getAvailableDiskSpace(path) Returns the available disk space based on a given path. On Windows, path must be a directory; on POSIX systems, it can be a file or directory.
getcwd() Get the current working directory.
getLinkAttributes(name) If the given file is a symbolic link, then this returns the attributes of the symbolic link itself rather than file that it points to. If the given file is not a symbolic link, then this function returns the same result as getAttributes.
getSize(name) Get size of file name in bytes.
getTimes(name, accessTime, modificationTime) Get the access and modified times of file or folder name.
getTimesWin(name, fileCreationTime, fileAccessTime, fileModificationTime) This function is Windows-Only.
isDir(name) Returns whether the given file is a directory.
isFile(name) Returns whether the given file (or directory) is a file.
isSymlink(name) Returns whether the given file is a symbolic link.
mkdir(pathname) Make a new directory pathname.
mkdirRecurse(pathname) Make directory and all parent directories as needed.
read(name, upTo) Read entire contents of file name and returns it as an untyped array. If the file size is larger than upTo, only upTo bytes are read.
readLink(link) This function is POSIX-Only.
readText(name) Reads and validates (using validate) a text file. S can be an array of any character type. However, no width or endian conversions are performed. So, if the width or endianness of the characters in the given file differ from the width or endianness of the element type of S, then validation will fail.
remove(name) Delete file name.
rename(from, to) Rename file from to to, moving it between directories if required. If the target file exists, it is overwritten.
rmdir(pathname) Remove directory pathname.
rmdirRecurse(pathname) Remove directory and all of its content and subdirectories, recursively.
setAttributes(name, attributes) Set the attributes of the given file.
setTimes(name, accessTime, modificationTime) Set access/modified times of file or folder name.
slurp(filename, format) Reads a file line by line and parses the line into a single value or a Tuple of values depending on the length of Types. The lines are parsed using the specified format string. The format string is passed to std.format.formattedRead, and therefore must conform to the format string specification outlined in std.format.
symlink(original, link) This function is POSIX-Only.
tempDir() Returns the path to a directory for temporary files. On POSIX platforms, it searches through the following list of directories and returns the first one which is found to exist:
  1. The directory given by the TMPDIR environment variable.
  2. The directory given by the TEMP environment variable.
  3. The directory given by the TMP environment variable.
  4. /tmp/
  5. /var/tmp/
  6. /usr/tmp/
thisExePath() Returns the full path of the current executable.
timeLastAccessed(statbuf) This function is POSIX-Only.
timeLastModified(name) Returns the time that the given file was last modified.
timeLastModified(name, returnIfMissing) Returns the time that the given file was last modified. If the file does not exist, returns returnIfMissing.
timeLastModified(statbuf) This function is POSIX-Only.
timeStatusChanged(statbuf) This function is POSIX-Only.
write(name, buffer) Write buffer to file name.

Classes

NameDescription
FileException Exception thrown for file I/O errors.

Structs

NameDescription
DirEntry Info on a file, similar to what you'd get from stat on a POSIX system.

Enums

NameDescription
SpanMode Dictates directory spanning policy for dirEntries (see below).

Global variables

NameTypeDescription
preserveAttributesDefault Flag!("preserveAttributes") Defaults to Yes.preserveAttributes on Windows, and the opposite on all other platforms.

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis

License

Boost License 1.0.