D 1.0   D 2.0
About Japanese Translation

www.digitalmars.com
Last update Sat Aug 30 12:38:01 2008

std.file

ファイル操作やディレクトリ走査のためのユーティリティです。

Authors:
Walter Bright, Andrei Alexandrescu

class FileException: object.Exception;
ファイル入出力エラーが発生したときに投げられる例外です。

void[] read(in string name);
ファイル name を読み取り、読み込まれたbyteの配列を返します。

Throws:
FileException をエラー時に投げる

void write(in string name, const void[] buffer);
ファイル name[] に buffer[] を書き込みます。

Throws:
FileException をエラー時に投げる

void append(in string name, in void[] buffer);
ファイル name[] を buffer[] に追記します。

Throws:
FileException をエラー時に投げる

void rename(in string from, in string to);
ファイル from[] を to[] に改名します。

Throws:
FileException をエラー時に投げる

void remove(in string name);
ファイル name[] を削除します。

Throws:
FileException をエラー時に投げる

ulong getSize(in string name);
ファイル name[] のサイズを取得します。

Throws:
FileException をエラー時に投げる

void getTimes(in string name, out d_time ftc, out d_time fta, out d_time ftm);
ファイル name[] の 作成/最終アクセス/更新日時を返します。

Throws:
FileException をエラー時に投げる

int exists(string name);
ファイル(またはディレクトリ) name[] が存在するかどうか 存在するなら 1 しないなら 0 を返します。

uint getAttributes(string name);
ファイル name[] の属性を得ます。

Throws:
FileException をエラー時に投げる

int isfile(in string name);
name[] はファイルか?

Throws:
name[] が存在しなければ FileException を投げる

int isdir(in string name);
name[] はディレクトリか?

Throws:
name[] が存在しなければ FileException を投げる

void chdir(in string pathname);
カレントディレクトリを pathname[] に変更

Throws:
FileException をエラー時に投げる

void mkdir(in string pathname);
ディレクトリ pathname[] を作成

Throws:
FileException をエラー時に投げる

void mkdirRecurse(string pathname);
ディレクトリと、もし存在しなければその親ディレクトリを再帰的に作成

void rmdir(in string pathname);
ディレクトリ pathname[] を削除

Throws:
FileException をエラー時に投げる

void rmdirRecurse(string pathname);
ディレクトリ及びその中身とサブディレクトリを 再帰的に削除

string getcwd();
カレントディレクトリを取得

Throws:
FileException をエラー時に投げる

struct DirEntry;
ディレクトリエントリ

string name;
ファイルかディレクトリの名前

ulong size;
ファイルのバイト数

d_time creationTime;
ファイル作成日時

d_time lastAccessTime;
ファイル最終アクセス日時

d_time lastWriteTime;
ファイル最終更新日時

uint isdir();
DirEntryがディレクトリの場合非ゼロ

uint isfile();
DirEntryがファイルの場合非ゼロ

string[] listdir(string pathname);
ディレクトリの内容を返します。 内容の名前には pathname は含まれません。

Throws:
FileException をエラー時に投げる

Example:
このプログラムは、指定されたパス引数内の全てのファイルと サブディレクトリをリストアップします。
 import std.stdio;
 import std.file;

 void main(string[] args)
 {
    auto dirs = std.file.listdir(args[1]);

    foreach (d; dirs)
	writefln(d);
 }


string[] listdir(string pathname, string pattern);
string[] listdir(string pathname, RegExp r);
ディレクトリとサブディレクトリにある全てのファイルのうち、 pattern または正規表現 r にマッチするもののみを返します。

Params:
string pathname ディレクトリ名
string pattern ワイルドカード入り文字列。例えば "*.d" 対応しているワイルドカード文字列は、 std.path の fnmatch() に記述されています。
r 正規表現です。より強力なパターンマッチに使用します。

Example:
このプログラムは、第一引数として渡されたパス上にある 拡張子 "d" のファイルを全てリストアップします。
 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);
 }
"d" もしくは "obj" の拡張子を持つファイルを探す、 正規表現バージョンです:
 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(in string pathname, bool delegate(string filename) callback);
pathname[] 内の全てのファイルとディレクトリ名を、 callback delegate に渡します。

Note:
この関数は非推奨となりました。新しいコードでは dirEntries (下参照) を使用してください。

Params:
bool delegate(string filename) callback 毎回それぞれのパス文字列を処理する Delegate。 処理を続行するには true を、 終了するには false を返すこと。

Example:
このプログラムは、自身を含めパス引数以下の全てのファイルを リストアップします。
 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(in string pathname, bool delegate(DirEntry* de) callback);
pathname[] 内の全てのファイルとディレクトリのDirEntryを、 callback delegate に渡します。

Note:
この関数は非推奨となりました。新しいコードでは dirEntries (下参照) を使用してください。

Params:
bool delegate(DirEntry* de) callback 毎回それぞれのパス文字列を処理する Delegate。 処理を続行するには true を、 終了するには false を返すこと。

Example:
このプログラムは、自身を含めパス引数以下の全てのファイルを リストアップします。
 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(in string s);
Win9x は "W" API をサポートしないため、 まず文字列を wchar に変換してから 現在のコードページに従いマルチバイト文字列に変換します。 (yaneuraoに感謝)

Deprecated:
変わりに std.windows.charset.toMBSz の使用が推奨されています。

void copy(in string from, in string to);
ファイル from[] を to[] へコピーします。

enum SpanMode;
dirEntries (下を参照) に指定するディレクトリ展開モードの指定

shallow
ディレクトリ1つの中身だけを列挙します(サブディレクトリには潜りません)

depth
ディレクトリを深さ優先(※訳注: 帰りがけ順?)で列挙します。つまり、 サブディレクトリの中身がサブディレクトリ自身より先に列挙されます。 例として、再帰的にファイルを削除する場合などに便利です。

breadth
ディレクトリを幅優先(※訳注: 行きがけ順?)で列挙します。つまり、 サブディレクトリ自身の直後に中身列挙が行われます。

DirIterator dirEntries(string path, SpanMode mode);
foreachでディレクトリの内容を列挙します。foreachで情報を受け取る変数の型は 名前のみ必要なら string、追加情報まで必要なら DirEntry とします。引数 mode は、 ディレクトリをたどる順番の指定です。ファイル名として返される文字列には、 path が接頭辞として含まれます。

Example:
 // ディレクトリ内をdepthモードで列挙
 foreach (string name; dirEntries("destroy/me", SpanMode.depth))
 {
     remove(name);
 }
 // ディレクトリ内をbreadthモードで列挙
 foreach (string name; dirEntries(".", SpanMode.breadth))
 {
     writeln(name);
 }
 // ディレクトリ内を詳細な情報付きで列挙
 foreach (DirEntry e; dirEntries("dmd-testing", SpanMode.breadth))
 {
     writeln(e.name, "\t", e.size);
 }