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.

Category Functions
General exists isDir isFile isSymlink rename thisExePath
Directories chdir dirEntries getcwd mkdir mkdirRecurse rmdir rmdirRecurse tempDir
Files append copy read readText remove slurp write
Symlinks symlink readLink
Attributes attrIsDir attrIsFile attrIsSymlink getAttributes getLinkAttributes getSize setAttributes
Timestamp getTimes getTimesWin setTimes timeLastModified
Other DirEntry FileException PreserveAttributes SpanMode

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

Name Description
append Appends buffer to file name.
attrIsDir Returns whether the given file attributes are for a directory.
attrIsFile Returns whether the given file attributes are for a file.
attrIsSymlink Returns whether the given file attributes are for a symbolic link.
chdir Change directory to pathname.
copy 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 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 path.
exists Determine whether the given file (or directory) exists.
getAttributes Returns the attributes of the given file.
getcwd Get the current working directory.
getLinkAttributes 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 Get size of file name in bytes.
getTimes Get the access and modified times of file or folder name.
getTimesWin This function is Windows-Only.
isDir Returns whether the given file is a directory.
isFile Returns whether the given file (or directory) is a file.
isSymlink Returns whether the given file is a symbolic link.
mkdir Make directory pathname.
mkdirRecurse Make directory and all parent directories as needed.
read 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 This function is Posix-Only.
readText Read and validates (using std.utf.validate) a text file. S can be a type of array of characters of any width and constancy. No width conversion is performed; if the width of the characters in file name is different from the width of elements of S, validation will fail.
remove Delete file name.
rename Rename file from to to. If the target file exists, it is overwritten.
rmdir Remove directory pathname.
rmdirRecurse Remove directory and all of its content and subdirectories, recursively.
setAttributes Set the attributes of the given file.
setTimes Set access/modified times of file or folder name.
slurp Reads a file line by line and parses the line into a single value or a std.typecons.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 This function is Posix-Only.
tempDir Returns the path to a directory for temporary files.
thisExePath Returns the full path of the current executable.
timeLastModified Returns the time that the given file was last modified.
timeLastModified Returns the time that the given file was last modified. If the file does not exist, returns returnIfMissing.
write Write buffer to file name.

Classes

Name Description
FileException Exception thrown for file I/O errors.

Structs

Name Description
DirEntry Info on a file, similar to what you'd get from stat on a Posix system.

Enums

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

Global variables

Name Type Description
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.

Comments