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.

Types 6

classFileException : Exception

Exception thrown for file I/O errors.

Fields
uint errnoOS error code.
Constructors
this(scope const(char)[] name, scope const(char)[] msg, string file, size_t line, uint errno)
this(scope const(char)[] name, scope const(char)[] msg, string file = __FILE__, size_t line = __LINE__)Constructor which takes an error message.
aliasPreserveAttributes = Flag!"preserveAttributes"

Dictates directory spanning policy for dirEntries (see below).

shallowOnly spans one directory.
depthSpans the directory in en.wikipedia.org/wiki/Tree_traversal#Post-order, i.e. the content of any subdirectory is spanned before that subdirectory itself. Useful e.g. when recursively deleting files.
breadthSpans the directory in en.wikipedia.org/wiki/Tree_traversal#Pre-order, i.e. the content of any subdirectory is spanned right after that subdirectory itself.
private structDirIteratorImpl
Fields
SpanMode _mode
bool _followSymlink
DirEntry _cur
DirHandle[] _stack
DirEntry[] _stashed
string _pathPrefix
Methods
void pushExtra(DirEntry de)
bool hasExtra()
DirEntry popExtra()
bool empty() @property
DirEntry front() @property
void popFront()
Constructors
this(string pathname, SpanMode mode, bool followSymlink)
Destructors
struct_DirIterator(bool useDIP1000)
Fields
SafeRefCounted!(DirIteratorImpl, RefCountedAutoInitialize.no) impl
Methods
bool empty() @property @trusted
DirEntry front() @property @trusted
void popFront() @trusted
Constructors
this(string pathname, SpanMode mode, bool followSymlink)
aliasDirIterator = _DirIterator!dip1000Enabled

Functions 63

fnstring deleteme() @property @safe
private fnT cenforce(T)(T condition, lazy scope const(char)[] name, string file = __FILE__, size_t line = __LINE__)
fnvoid[] 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.
fnvoid[] read(R)(auto ref R name, size_t upTo = size_t.max) if (isConvertibleToString!R)ditto
fnS 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...
fnvoid write(R)(R name, const void[] buffer) if ((isSomeFiniteCharInputRange!R || isSomeString!R) && !isConvertibleToString!R)Write `buffer` to file `name`.
fnvoid write(R)(auto ref R name, const void[] buffer) if (isConvertibleToString!R)ditto
fnvoid append(R)(R name, const void[] buffer) if ((isSomeFiniteCharInputRange!R || isSomeString!R) && !isConvertibleToString!R)Appends `buffer` to file `name`.
fnvoid append(R)(auto ref R name, const void[] buffer) if (isConvertibleToString!R)ditto
fnvoid 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.
fnvoid rename(RF, RT)(auto ref RF from, auto ref RT to) if (isConvertibleToString!RF || isConvertibleToString!RT)ditto
private fnvoid renameImpl(scope const(char)[] f, scope const(char)[] t, scope const(FSChar) * fromz, scope const(FSChar) * toz) @trusted
fnvoid remove(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Delete file `name`.
fnvoid remove(R)(auto ref R name) if (isConvertibleToString!R)ditto
private fnvoid removeImpl(scope const(char)[] name, scope const(FSChar) * namez) @trusted
fnulong getSize(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Get size of file `name` in bytes.
fnulong getSize(R)(auto ref R name) if (isConvertibleToString!R)ditto
fnvoid 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`.
fnvoid getTimes(R)(auto ref R name, out SysTime accessTime, out SysTime modificationTime) if (isConvertibleToString!R)ditto
fnvoid setTimes(R)(R name, SysTime accessTime, SysTime modificationTime) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Set access/modified times of file or folder `name`.
fnvoid setTimes(R)(auto ref R name, SysTime accessTime, SysTime modificationTime) if (isConvertibleToString!R)ditto
private fnvoid setTimesImpl(scope const(char)[] names, scope const(FSChar) * namez, SysTime accessTime, SysTime modificationTime) @trusted
fnSysTime timeLastModified(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Returns the time that the given file was last modified.
fnSysTime timeLastModified(R)(auto ref R name) if (isConvertibleToString!R)ditto
fnSysTime 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`.
fnbool 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 exists
fnbool exists(R)(auto ref R name) if (isConvertibleToString!R)ditto
private fnbool existsImpl(scope const(FSChar) * namez) @trusted nothrow @nogc
fnuint getAttributes(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Returns the attributes of the given file.
fnuint getAttributes(R)(auto ref R name) if (isConvertibleToString!R)ditto
fnuint 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...
fnuint getLinkAttributes(R)(auto ref R name) if (isConvertibleToString!R)ditto
fnvoid setAttributes(R)(R name, uint attributes) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Set the attributes of the given file.
fnvoid setAttributes(R)(auto ref R name, uint attributes) if (isConvertibleToString!R)ditto
fnbool isDir(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R) @propertyReturns whether the given file is a directory.
fnbool isDir(R)(auto ref R name) if (isConvertibleToString!R) @propertyditto
fnbool attrIsDir(uint attributes) @safe pure nothrow @nogcReturns whether the given file attributes are for a directory.
fnbool isFile(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R) @propertyReturns whether the given file (or directory) is a file.
fnbool isFile(R)(auto ref R name) if (isConvertibleToString!R) @propertyditto
fnbool attrIsFile(uint attributes) @safe pure nothrow @nogcReturns whether the given file attributes are for a file.
fnbool isSymlink(R)(R name) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R) @propertyReturns whether the given file is a symbolic link.
fnbool isSymlink(R)(auto ref R name) if (isConvertibleToString!R) @propertyditto
fnbool attrIsSymlink(uint attributes) @safe pure nothrow @nogcReturns whether the given file attributes are for a symbolic link.
fnvoid chdir(R)(R pathname) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Change directory to `pathname`. Equivalent to `cd` on Windows and POSIX.
fnvoid chdir(R)(auto ref R pathname) if (isConvertibleToString!R)ditto
fnvoid mkdir(R)(R pathname) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Make a new directory `pathname`.
fnvoid mkdir(R)(auto ref R pathname) if (isConvertibleToString!R)ditto
private fnbool ensureDirExists()(scope const(char)[] pathname)
fnvoid mkdirRecurse(scope const(char)[] pathname) @safeMake directory and all parent directories as needed.
fnvoid rmdir(R)(R pathname) if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R)Remove directory `pathname`.
fnvoid rmdir(R)(auto ref R pathname) if (isConvertibleToString!R)ditto
fnstring thisExePath() @trustedReturns the full path of the current executable.
fnvoid 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 ...
fnvoid copy(RF, RT)(auto ref RF from, auto ref RT to, PreserveAttributes preserve = preserveAttributesDefault) if (isConvertibleToString!RF || isConvertibleToString!RT)ditto
private fnvoid copyImpl(scope const(char)[] f, scope const(char)[] t, scope const(FSChar) * fromz, scope const(FSChar) * toz, PreserveAttributes preserve) @trusted
fnvoid rmdirRecurse(scope const(char)[] pathname) @safeRemove directory and all of its content and subdirectories, recursively.
fnvoid rmdirRecurse(ref scope DirEntry de) @safeditto
fnvoid rmdirRecurse(scope DirEntry de) @safeditto
fnauto 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, ...
fnauto dirEntries(bool useDIP1000 = dip1000Enabled)(string path, string pattern, SpanMode mode, bool followSymlink = true)Ditto
fnSelect!(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 ...
fnstring 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...
fnulong 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.