- Comments
- D
- Search
- Home

Last update Sun Jul 1 11:16:36 2007

# std.file

class FileException: object.Exception;
  Exception thrown for file I/O errors.
void[] read(const char[] name);
  Read file name[], return array of bytes read.
  Throws:
  FileException on error.
void write(const char[] name, const void[] buffer);
  Write buffer[] to file name[].
  Throws:
  FileException on error.
void append(const char[] name, const void[] buffer);
  Append buffer[] to file name[].
  Throws:
  FileException on error.
void rename(const char[] from, const char[] to);
  Rename file from[] to to[].
  Throws:
  FileException on error.
void remove(const char[] name);
  Delete file name[].
  Throws:
  FileException on error.
ulong getSize(const char[] name);
  Get size of file name[].
  Throws:
  FileException on error.
void getTimes(const char[] name, out long ftc, out long fta, out long ftm);
  Get creation/access/modified times of file name[].
  Throws:
  FileException on error.
int exists(const(char)[] name);
  Does file name[] (or directory) exist? Return 1 if it does, 0 if not.
uint getAttributes(const(char)[] name);
  Get file name[] attributes.
  Throws:
  FileException on error.
int isfile(const char[] name);
  Is name[] a file?
  Throws:
  FileException if name[] doesn't exist.
int isdir(const char[] name);
  Is name[] a directory?
  Throws:
  FileException if name[] doesn't exist.
void chdir(const char[] pathname);
  Change directory to pathname[].
  Throws:
  FileException on error.
void mkdir(const char[] pathname);
  Make directory pathname[].
  Throws:
  FileException on error.
void rmdir(const char[] pathname);
  Remove directory pathname[].
  Throws:
  FileException on error.
const(char)[] getcwd();
  Get current directory.
  Throws:
  FileException on error.
struct DirEntry;
  Directory Entry
  const(char)[] name;
    file or directory name
  ulong size;
    size of file in bytes
  long creationTime;
    time of file creation
  long lastAccessTime;
    time file was last accessed
  long lastWriteTime;
    time file was last written to
  uint isdir();
    Return !=0 if DirEntry is a directory.
  uint isfile();
    Return !=0 if DirEntry is a file.
const(char)[][] listdir(const(char)[] pathname);
  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);
       }

const(char)[][] listdir(const char[] pathname, const char[] pattern);
const(char)[][] listdir(const char[] pathname, RegExp r);
  Return all the files in the directory and its subdirectories that match pattern or regular expression r.
  Params:

  const char[] pathname | Directory name
  const char[] 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);
       }

void listdir(const char[] pathname, bool delegate(const char[] filename) callback);
  For each file and directory name in pathname[], pass it to the callback delegate.
  Params:

  bool delegate(const 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);
       }

void listdir(const char[] pathname, bool delegate(DirEntry* de) callback);
  For each file and directory DirEntry in pathname[], pass it to the callback delegate.
  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);
       }

const(char)* toMBSz(const char[] s);
  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.
void copy(const char[] from, const char[] to);
  Copy a file from[] to[].

Copyright © 1999-2007 by Digital Mars, All Rights Reserved | Page generated by Ddoc.