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.

Members

Classes

FileException
class FileException

Exception thrown for file I/O errors.

Enums

SpanMode
enum SpanMode

Dictates directory spanning policy for dirEntries (see below).

Functions

append
void append(R name, void[] buffer)

Appends buffer to file name.

attrIsDir
bool attrIsDir(uint attributes)

Returns whether the given file attributes are for a directory.

attrIsFile
bool attrIsFile(uint attributes)

Returns whether the given file attributes are for a file.

attrIsSymlink
bool attrIsSymlink(uint attributes)

Returns whether the given file attributes are for a symbolic link.

chdir
void chdir(R pathname)

Change directory to pathname. Equivalent to cd on Windows and POSIX.

copy
void copy(RF from, RT to, PreserveAttributes 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
auto dirEntries(string path, SpanMode mode, bool followSymlink)
auto dirEntries(string path, string pattern, SpanMode mode, bool 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
bool exists(R name)

Determine whether the given file (or directory) exists.

getAttributes
uint getAttributes(R name)

Returns the attributes of the given file.

getAvailableDiskSpace
ulong getAvailableDiskSpace(const(char)[] 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.

getLinkAttributes
uint getLinkAttributes(R 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
ulong getSize(R name)

Get size of file name in bytes.

getTimes
void getTimes(R name, SysTime accessTime, SysTime modificationTime)

Get the access and modified times of file or folder name.

getTimesWin
void getTimesWin(R name, SysTime fileCreationTime, SysTime fileAccessTime, SysTime fileModificationTime)

This function is Windows-Only.

getcwd
string getcwd()

Get the current working directory.

mkdir
void mkdir(R pathname)

Make a new directory pathname.

mkdirRecurse
void mkdirRecurse(const(char)[] pathname)

Make directory and all parent directories as needed.

read
void[] read(R name, size_t 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
string readLink(R link)

This function is POSIX-Only.

readText
S readText(R name)

Reads and validates (using std.utf.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
void remove(R name)

Delete file name.

rename
void rename(RF from, RT to)

Rename file from to to, moving it between directories if required. If the target file exists, it is overwritten.

rmdir
void rmdir(R pathname)

Remove directory pathname.

rmdirRecurse
void rmdirRecurse(const(char)[] pathname)
void rmdirRecurse(DirEntry de)

Remove directory and all of its content and subdirectories, recursively.

setAttributes
void setAttributes(R name, uint attributes)

Set the attributes of the given file.

setTimes
void setTimes(R name, SysTime accessTime, SysTime modificationTime)

Set access/modified times of file or folder name.

slurp
Select!(Types.length == 1, Types[0][], Tuple!(Types)[]) slurp(string filename, const(char)[] format)

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
void symlink(RO original, RL link)

This function is POSIX-Only.

tempDir
string 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
string thisExePath()

Returns the full path of the current executable.

timeLastAccessed
SysTime timeLastAccessed(stat_t statbuf)

This function is POSIX-Only.

timeLastModified
SysTime timeLastModified(R name)

Returns the time that the given file was last modified.

timeLastModified
SysTime timeLastModified(R name, SysTime returnIfMissing)

Returns the time that the given file was last modified. If the file does not exist, returns returnIfMissing.

timeLastModified
SysTime timeLastModified(stat_t statbuf)

This function is POSIX-Only.

timeStatusChanged
SysTime timeStatusChanged(stat_t statbuf)

This function is POSIX-Only.

write
void write(R name, void[] buffer)

Write buffer to file name.

Properties

isDir
bool isDir [@property getter]

Returns whether the given file is a directory.

isFile
bool isFile [@property getter]

Returns whether the given file (or directory) is a file.

isSymlink
bool isSymlink [@property getter]

Returns whether the given file is a symbolic link.

Structs

DirEntry
struct DirEntry

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

Variables

preserveAttributesDefault
PreserveAttributes preserveAttributesDefault;

Defaults to Yes.preserveAttributes on Windows, and the opposite on all other platforms.

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.

Meta