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.
| Category | Functions | 
|---|---|
| General | existsisDirisFileisSymlinkrenamethisExePath | 
| Directories | chdirdirEntriesgetcwdmkdirmkdirRecursermdirrmdirRecursetempDir | 
| Files | appendcopyreadreadTextremoveslurpwrite | 
| Symlinks | symlinkreadLink | 
| Attributes | attrIsDirattrIsFileattrIsSymlinkgetAttributesgetLinkAttributesgetSizesetAttributes | 
| Timestamp | getTimesgetTimesWinsetTimestimeLastModifiedtimeLastAccessedtimeStatusChanged | 
| Other | DirEntryFileExceptionPreserveAttributesSpanModegetAvailableDiskSpace | 
See Also
The official tutorial for an
introduction to working with files in D, module
std for opening files and manipulating them via handles,
and module std for manipulating path strings.
Functions
| Name | Description | 
|---|---|
| 
									append(name, buffer)
								 | Appends bufferto filename. | 
| 
									attrIsDir(attributes)
								 | Returns whether the given file attributes are for a directory. | 
| 
									attrIsFile(attributes)
								 | Returns whether the given file attributes are for a file. | 
| 
									attrIsSymlink(attributes)
								 | Returns whether the given file attributes are for a symbolic link. | 
| 
									chdir(pathname)
								 | Change directory to pathname. Equivalent tocdon
Windows and POSIX. | 
| 
									copy(from, to, preserve)
								 | Copy file fromto fileto. File timestamps are preserved.
File attributes are preserved, ifpreserveequalsYes.
On Windows onlyYes(the default on Windows) is supported.
If the target file exists, it is overwritten. | 
| 
									dirEntries(path, mode, followSymlink)
								 | Returns an input range
    of DirEntrythat lazily iterates a given directory,
    also provides two ways of foreach iteration. The iteration variable can be of
    typestringif only the name is needed, orDirEntryif 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(name)
								 | Determine whether the given file (or directory) exists. | 
| 
									getAttributes(name)
								 | Returns the attributes of the given file. | 
| 
									getAvailableDiskSpace(path)
								 | Returns the available disk space based on a given path.
On Windows, pathmust be a directory; on POSIX systems, it can be a file or directory. | 
| 
									getcwd()
								 | Get the current working directory. | 
| 
									getLinkAttributes(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(name)
								 | Get size of file namein bytes. | 
| 
									getTimes(name, accessTime, modificationTime)
								 | Get the access and modified times of file or folder name. | 
| 
									getTimesWin(name, fileCreationTime, fileAccessTime, fileModificationTime)
								 | This function is Windows-Only. | 
| 
									isDir(name)
								 | Returns whether the given file is a directory. | 
| 
									isFile(name)
								 | Returns whether the given file (or directory) is a file. | 
| 
									isSymlink(name)
								 | Returns whether the given file is a symbolic link. | 
| 
									mkdir(pathname)
								 | Make a new directory pathname. | 
| 
									mkdirRecurse(pathname)
								 | Make directory and all parent directories as needed. | 
| 
									read(name, upTo)
								 | Read entire contents of file nameand returns it as an untyped
array. If the file size is larger thanupTo, onlyupTobytes are read. | 
| 
									readLink(link)
								 | This function is POSIX-Only. | 
| 
									readText(name)
								 | 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 the given
    file differ from the width or endianness of the element type of S, then
    validation will fail. | 
| 
									remove(name)
								 | Delete file name. | 
| 
									rename(from, to)
								 | Rename file fromtoto, moving it between directories if required.
 If the target file exists, it is overwritten. | 
| 
									rmdir(pathname)
								 | Remove directory pathname. | 
| 
									rmdirRecurse(pathname)
								 | Remove directory and all of its content and subdirectories, recursively. | 
| 
									setAttributes(name, attributes)
								 | Set the attributes of the given file. | 
| 
									setTimes(name, accessTime, modificationTime)
								 | Set access/modified times of file or folder name. | 
| 
									slurp(filename, format)
								 | Reads a file line by line and parses the line into a single value or a Tupleof values depending on the length ofTypes.
 The lines are parsed using the specified format string. The format string is
 passed tostd, and therefore must conform to the
 format string specification outlined instd. | 
| 
									symlink(original, link)
								 | This function is POSIX-Only. | 
| 
									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: 
 | 
| 
									thisExePath()
								 | Returns the full path of the current executable. | 
| 
									timeLastAccessed(statbuf)
								 | This function is POSIX-Only. | 
| 
									timeLastModified(name)
								 | Returns the time that the given file was last modified. | 
| 
									timeLastModified(name, returnIfMissing)
								 | Returns the time that the given file was last modified. If the
    file does not exist, returns returnIfMissing. | 
| 
									timeLastModified(statbuf)
								 | This function is POSIX-Only. | 
| 
									timeStatusChanged(statbuf)
								 | This function is POSIX-Only. | 
| 
									write(name, buffer)
								 | Write bufferto filename. | 
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 Yeson Windows, and the opposite on all other platforms. | 
Authors
License
					Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.