See Also
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.
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 |
| Directories | chdir
dirEntries getcwd mkdir mkdirRecurse rmdir rmdirRecurse tempDir |
| Files | append |
| Symlinks | symlink |
| Attributes | attrIsDir
attrIsFile attrIsSymlink getAttributes getLinkAttributes getSize setAttributes |
| Timestamp | getTimes
getTimesWin setTimes timeLastModified timeLastAccessed timeStatusChanged |
| Other | DirEntry
FileException PreserveAttributes SpanMode getAvailableDiskSpace |
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.
Exception thrown for file I/O errors.
uint errnoOS error code.Dictates directory spanning policy for dirEntries (see below).
SafeRefCounted!(DirIteratorImpl, RefCountedAutoInitialize.no) implT cenforce(T)(T condition, lazy scope const(char)[] name, string file = __FILE__, size_t line = __LINE__)void[] read(R)(R name, size_t upTo = size_t.max) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)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.S readText(S = string, R)(auto ref R name) if (isSomeString!S && (isSomeFiniteCharInputRange!R || is(StringTypeOf!R)))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...void write(R)(R name, const void[] buffer) if ((isSomeFiniteCharInputRange!R || isSomeString!R) && !isConvertibleToString!R)Write `buffer` to file `name`.void append(R)(R name, const void[] buffer) if ((isSomeFiniteCharInputRange!R || isSomeString!R) && !isConvertibleToString!R)Appends `buffer` to file `name`.void rename(RF, RT)(RF from, RT to) if ((isSomeFiniteCharInputRange!RF || isSomeString!RF) && !isConvertibleToString!RF &&
(isSomeFiniteCharInputRange!RT || isSomeString!RT) && !isConvertibleToString!RT)Rename file `from` to `to`, moving it between directories if required. If the target file exists, it is overwritten.void rename(RF, RT)(auto ref RF from, auto ref RT to) if (isConvertibleToString!RF || isConvertibleToString!RT)dittovoid renameImpl(scope const(char)[] f, scope const(char)[] t,
scope const(FSChar) * fromz, scope const(FSChar) * toz) @trustedvoid remove(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Delete file `name`.ulong getSize(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Get size of file `name` in bytes.void getTimes(R)(R name,
out SysTime accessTime,
out SysTime modificationTime) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Get the access and modified times of file or folder `name`.void getTimes(R)(auto ref R name,
out SysTime accessTime,
out SysTime modificationTime) if (isConvertibleToString!R)dittovoid setTimes(R)(R name,
SysTime accessTime,
SysTime modificationTime) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Set access/modified times of file or folder `name`.void setTimes(R)(auto ref R name,
SysTime accessTime,
SysTime modificationTime) if (isConvertibleToString!R)dittovoid setTimesImpl(scope const(char)[] names, scope const(FSChar) * namez,
SysTime accessTime, SysTime modificationTime) @trustedSysTime timeLastModified(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Returns the time that the given file was last modified.SysTime timeLastModified(R)(R name, SysTime returnIfMissing) if (isSomeFiniteCharInputRange!R)Returns the time that the given file was last modified. If the file does not exist, returns `returnIfMissing`.bool exists(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Determine whether the given file (or directory) exists. Params: name = string or range of characters representing the file name Returns: true if the file name specified as input existsuint getAttributes(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Returns the attributes of the given file.uint getLinkAttributes(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)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 r...void setAttributes(R)(R name, uint attributes) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Set the attributes of the given file.bool isDir(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R) @propertyReturns whether the given file is a directory.bool attrIsDir(uint attributes) @safe pure nothrow @nogcReturns whether the given file attributes are for a directory.bool isFile(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R) @propertyReturns whether the given file (or directory) is a file.bool attrIsFile(uint attributes) @safe pure nothrow @nogcReturns whether the given file attributes are for a file.bool isSymlink(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R) @propertyReturns whether the given file is a symbolic link.bool attrIsSymlink(uint attributes) @safe pure nothrow @nogcReturns whether the given file attributes are for a symbolic link.void chdir(R)(R pathname) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Change directory to `pathname`. Equivalent to `cd` on Windows and POSIX.void mkdir(R)(R pathname) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Make a new directory `pathname`.void mkdirRecurse(scope const(char)[] pathname) @safeMake directory and all parent directories as needed.void rmdir(R)(R pathname) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Remove directory `pathname`.void copy(RF, RT)(RF from, RT to, PreserveAttributes preserve = preserveAttributesDefault) if (isSomeFiniteCharInputRange!RF && !isConvertibleToString!RF &&
isSomeFiniteCharInputRange!RT && !isConvertibleToString!RT)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 ...void copy(RF, RT)(auto ref RF from, auto ref RT to, PreserveAttributes preserve = preserveAttributesDefault) if (isConvertibleToString!RF || isConvertibleToString!RT)dittovoid copyImpl(scope const(char)[] f, scope const(char)[] t,
scope const(FSChar) * fromz, scope const(FSChar) * toz,
PreserveAttributes preserve) @trustedvoid rmdirRecurse(scope const(char)[] pathname) @safeRemove directory and all of its content and subdirectories, recursively.auto dirEntries(bool useDIP1000 = dip1000Enabled)(string path, SpanMode mode, bool followSymlink = true)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, ...auto dirEntries(bool useDIP1000 = dip1000Enabled)(string path, string pattern, SpanMode mode,
bool followSymlink = true)DittoSelect!(Types.length == 1, Types[0][], Tuple!(Types)[]) slurp(Types...)(string filename, scope const(char)[] 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 ...string tempDir() @trustedReturns 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: The directory given...ulong getAvailableDiskSpace(scope const(char)[] path) @safeReturns 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.