std.file
Utilities for manipulating files and scanning directories.Authors:
Walter Bright, Andrei Alexandrescu
- Exception thrown for file I/O errors.
- Read file name, return array of bytes read.
Throws:
FileException on error.
- Write buffer[] to file name[].
Throws:
FileException on error.
- Append buffer[] to file name[].
Throws:
FileException on error.
- Rename file from[] to to[].
Throws:
FileException on error.
- Delete file name[].
Throws:
FileException on error.
- Get size of file name[].
Throws:
FileException on error.
- Get creation/access/modified times of file name.
Throws:
FileException on error.
- Does file name[] (or directory) exist?
Return 1 if it does, 0 if not.
- Get file name[] attributes.
Throws:
FileException on error.
- Is name[] a file?
Throws:
FileException if name[] doesn't exist.
- Is name[] a directory?
Throws:
FileException if name[] doesn't exist.
- Change directory to pathname[].
Throws:
FileException on error.
- Make directory pathname[].
Throws:
FileException on error.
- Remove directory pathname[].
Throws:
FileException on error.
- Get current directory.
Throws:
FileException on error.
- Directory Entry
- Return contents of directory pathname[].
The names in the contents do not include the pathname.
Throws:
FileException on error
Example:
This program lists all the files and subdirectories in its path argument.import std.stdio; import std.file; void main(string[] args) { auto dirs = std.file.listdir(args[1]); foreach (d; dirs) writefln(d); }
- Return all the files in the directory and its subdirectories
that match pattern or regular expression r.
Params:
string pathname Directory name string pattern String with wildcards, such as "*.d". The supported wildcard strings are described under fnmatch() in std.path. r Regular expression, for more powerful pattern matching.
Example:
This program lists all the files with a "d" extension in the path passed as the first argument.import std.stdio; import std.file; void main(string[] args) { auto d_source_files = std.file.listdir(args[1], "*.d"); foreach (d; d_source_files) writefln(d); }
A regular expression version that searches for all files with "d" or "obj" extensions:import std.stdio; import std.file; import std.regexp; void main(string[] args) { auto d_source_files = std.file.listdir(args[1], RegExp(r"\.(d|obj)$")); foreach (d; d_source_files) writefln(d); }
- For each file and directory name in pathname[],
pass it to the callback delegate.
Note:
This function is being phased off. New code should use dirEntries (see below).
Params:
bool delegate(invariant(char)[] filename) callback Delegate that processes each filename in turn. Returns true to continue, false to stop.
Example:
This program lists all the files in its path argument, including the path.import std.stdio; import std.path; import std.file; void main(string[] args) { auto pathname = args[1]; string[] result; bool listing(string filename) { result ~= std.path.join(pathname, filename); return true; // continue } listdir(pathname, &listing); foreach (name; result) writefln("%s", name); }
- For each file and directory DirEntry in pathname[],
pass it to the callback delegate.
Note:
This function is being phased off. New code should use dirEntries (see below).
Params:
bool delegate(DirEntry* de) callback Delegate that processes each DirEntry in turn. Returns true to continue, false to stop.
Example:
This program lists all the files in its path argument and all subdirectories thereof.import std.stdio; import std.file; void main(string[] args) { bool callback(DirEntry* de) { if (de.isdir) listdir(de.name, &callback); else writefln(de.name); return true; } listdir(args[1], &callback); }
- Since Win 9x does not support the "W" API's, first convert
to wchar, then convert to multibyte using the current code
page.
(Thanks to yaneurao for this)
Deprecated:
use std.windows.charset.toMBSz instead.
- Copy a file from[] to[].
- Dictates directory spanning policy for dirEntries (see below).
- Only spans one directory.
- Spans the directory depth-first, i.e. the content of any
subdirectory is spanned before that subdirectory itself. Useful
e.g. when recursively deleting files.
- Spans the directory breadth-first, i.e. the content of any
subdirectory is spanned right after that subdirectory itself.
- Iterates a directory using foreach. 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
the how the directory is traversed. The name of the directory entry
includes the path prefix.
Example:
// Iterate a directory in depth foreach (string name; dirEntries("destroy/me", SpanMode.depth)) { remove(name); } // Iterate a directory in breadth foreach (string name; dirEntries(".", SpanMode.breadth)) { writeln(name); } // Iterate a directory and get detailed info about it foreach (DirEntry e; dirEntries("dmd-testing", SpanMode.breadth)) { writeln(e.name, "\t", e.size); }