英語版
このページの英語版を見る
std.file
ファイルを操作し、ディレクトリをスキャンするためのユーティリティ。関数" を使用する。
このモジュールの関数は、ファイルを単位として扱う。
を一度に読み書きする。ファイルを開き、ハンドルで操作するには
モジュールを参照のこと。 std.stdio.
License:
Authors:
ソース std/file.d
- class
FileException
: object.Exception; - ファイルI/Oエラーの際にスローされる例外。Examples:
import std.exception : assertThrown; assertThrown!FileException("non.existing.file.".readText);
- immutable uint
errno
; - OSのエラーコード。
- pure @safe this(scope const(char)[]
name
, scope const(char)[]msg
, stringfile
= __FILE__, size_tline
= __LINE__); - エラーメッセージを受け取るコンストラクタ。Parameters:
const(char)[] name
エラーが発生したファイル名。 const(char)[] msg
エラーを説明するメッセージ。 string file
エラーが発生したファイル。 size_t line
エラーが発生した行 - @trusted this(scope const(char)[]
name
, uinterrno
= .errno
, stringfile
= __FILE__, size_tline
= __LINE__); - エラー番号を取るコンストラクタ(WindowsではGetLastError WindowsではGetLastError、POSIXではerrno)。Parameters:
const(char)[] name
エラーが発生したファイル名。 uint errno
エラー番号。 string file
エラーが発生したファイル。 デフォルトは__FILE__ 。 size_t line
エラーが発生した行。 デフォルトは__LINE__ 。
- void[]
read
(R)(Rname
, size_tupTo
= size_t.max)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
void[]read
(R)(auto ref Rname
, size_tupTo
= size_t.max)
if (isConvertibleToString!R); - ファイル
name
の内容全体を読み込み、それを型付きでない 配列として返す。ファイル・サイズがupTo
より大きい場合はupTo
バイトだけが読み込まれる。Parameters:R name
ファイル名を表す文字列または文字の範囲 size_t upTo
存在する場合、読み込む最大バイト数 Returns:読み込むバイト数の型付きでない配列。Throws:FileExceptionエラーになる。See Also:std.file.readTextテキストファイルの読み込みと検証を行う。Examples:import std.utf : byChar; scope(exit) { assert(exists(deleteme)); remove(deleteme); } std.file.write(deleteme, "1234"); // deleteemeは一時ファイルの名前である writeln(read(deleteme, 2)); // "12" writeln(read(deleteme.byChar)); // "1234" writeln((cast(const(ubyte)[])read(deleteme)).length); // 4
- S
readText
(S = string, R)(auto ref Rname
)
if (isSomeString!S && (isSomeFiniteCharInputRange!R || is(StringTypeOf!R))); - テキストファイルを読み込んで検証する。 std.utf.validateを使って)テキストファイルを読み込む。Sは は任意の文字型の配列である。ただし、幅変換やエンディアン変換は行われない。 は行われない。したがって、もし与えられた ファイルの文字の幅やエンディアンがSの要素型の幅やエンディアンと異なる場合、検証は失敗する。 のバリデーションは失敗する。Parameters:
S ファイルの文字列型 R name
ファイル名を表す文字列または文字の範囲 Returns:読み込んだ文字の配列。Throws:FileExceptionファイルの読み込みにエラーがある場合 std.utf.UTFExceptionUTFデコードエラーの場合See Also:std.file.readバイナリファイルを読み込む。Examples:UTF-8テキストでファイルを読み込む。write(deleteme, "abc"); // deleteemeは一時ファイルの名前である scope(exit) remove(deleteme); string content = readText(deleteme); writeln(content); // "abc"
- void
write
(R)(Rname
, const void[]buffer
)
if ((isSomeFiniteCharInputRange!R || isSomeString!R) && !isConvertibleToString!R);
voidwrite
(R)(auto ref Rname
, const void[]buffer
)
if (isConvertibleToString!R); - 書き込む
buffer
ファイルに書き込むname
.ファイルがまだ存在しなければ、ファイルを作成する。Parameters:R name
ファイル名を表す文字列または文字の範囲 void[] buffer
ファイルに書き込むデータ Throws:FileExceptionに書き込む。See Also:Examples:scope(exit) { assert(exists(deleteme)); remove(deleteme); } int[] a = [ 0, 1, 1, 2, 3, 5, 8 ]; write(deleteme, a); // deleteemeは一時ファイルの名前である const bytes = read(deleteme); const fileInts = () @trusted { return cast(int[]) bytes; }(); writeln(fileInts); // a
- void
append
(R)(Rname
, const void[]buffer
)
if ((isSomeFiniteCharInputRange!R || isSomeString!R) && !isConvertibleToString!R);
voidappend
(R)(auto ref Rname
, const void[]buffer
)
if (isConvertibleToString!R); - ファイルに追加する
buffer
ファイルに追加するname
.ファイルがまだ存在しない場合は作成する。Parameters:R name
ファイル名を表す文字列または文字の範囲 void[] buffer
ファイルに追加するデータ Throws:FileExceptionに追加される。Examples:scope(exit) { assert(exists(deleteme)); remove(deleteme); } int[] a = [ 0, 1, 1, 2, 3, 5, 8 ]; write(deleteme, a); // deleteemeは一時ファイルの名前である int[] b = [ 13, 21 ]; append(deleteme, b); const bytes = read(deleteme); const fileInts = () @trusted { return cast(int[]) bytes; }(); writeln(fileInts); // a ~ b
- void
rename
(RF, RT)(RFfrom
, RTto
)
if ((isSomeFiniteCharInputRange!RF || isSomeString!RF) && !isConvertibleToString!RF && (isSomeFiniteCharInputRange!RT || isSomeString!RT) && !isConvertibleToString!RT);
voidrename
(RF, RT)(auto ref RFfrom
, auto ref RTto
)
if (isConvertibleToString!RF || isConvertibleToString!RT); - ファイル名を変更する
from
に変更する。to
必要に応じてディレクトリ間を移動する。 ターゲット・ファイルが存在する場合は上書きされる。異なるマウントポイントやドライブ間でファイル名を変更することはできない。 またはドライブ間でファイル名を変更することはできない。POSIXでは、操作はアトミックである。つまりto
がすでに存在する場合、操作中に ここでto
が存在しない時間帯が存在しないことを意味する。参照 を参照のこと。 のマニュアルを参照のこと。Parameters:RF from
既存のファイル名を表す文字列または文字の範囲 RT to
目的のファイル名を表す文字列または文字の範囲 Throws:FileExceptionエラー時Examples:auto t1 = deleteme, t2 = deleteme~"2"; scope(exit) foreach (t; [t1, t2]) if (t.exists) t.remove(); t1.write("1"); t1.rename(t2); writeln(t2.readText); // "1" t1.write("2"); t1.rename(t2); writeln(t2.readText); // "2"
- void
remove
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
voidremove
(R)(auto ref Rname
)
if (isConvertibleToString!R); - ファイルを削除する
name
.Parameters:R name
ファイル名を表す文字列または文字の範囲 Throws:FileExceptionエラー時にExamples:import std.exception : assertThrown; deleteme.write("Hello"); writeln(deleteme.readText); // "Hello" deleteme.remove; assertThrown!FileException(deleteme.readText);
- ulong
getSize
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
ulonggetSize
(R)(auto ref Rname
)
if (isConvertibleToString!R); - ファイルのサイズを取得する
name
をバイト単位で取得する。Parameters:R name
ファイル名を表す文字列または文字の範囲 Returns:バイト単位のファイルサイズ。Throws:FileExceptionエラー時(ファイルが見つからないなど)。Examples:scope(exit) deleteme.remove; // サイズ1のファイルを作成する write(deleteme, "a"); writeln(getSize(deleteme)); // 1 // サイズ3のファイルを作成する write(deleteme, "abc"); writeln(getSize(deleteme)); // 3
- void
getTimes
(R)(Rname
, out SysTimeaccessTime
, out SysTimemodificationTime
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
voidgetTimes
(R)(auto ref Rname
, out SysTimeaccessTime
, out SysTimemodificationTime
)
if (isConvertibleToString!R); - ファイルやフォルダのアクセス時刻と更新時刻を取得する
name
.Parameters:R name
時間を取得するファイル/フォルダ名 SysTime accessTime
ファイル/フォルダーが最後にアクセスされた時刻。 SysTime modificationTime
ファイル/フォルダーが最後に変更された時間。 Throws:FileExceptionエラー時Examples:import std.datetime : abs, SysTime; scope(exit) deleteme.remove; write(deleteme, "a"); SysTime accessTime, modificationTime; getTimes(deleteme, accessTime, modificationTime); import std.datetime : Clock, seconds; auto currTime = Clock.currTime(); enum leeway = 5.seconds; auto diffAccess = accessTime - currTime; auto diffModification = modificationTime - currTime; assert(abs(diffAccess) <= leeway); assert(abs(diffModification) <= leeway);
- void
getTimesWin
(R)(Rname
, out SysTimefileCreationTime
, out SysTimefileAccessTime
, out SysTimefileModificationTime
)
if (isSomeFiniteCharInputRange!R || isConvertibleToString!R); この関数はWindows専用である。 ファイルの作成/アクセス/変更時刻を取得する。name
. これは、getTimes と同じだが、ファイルの作成時刻も取得できる。 の作成時刻も得ることができる点を除けば、これは と同じである。Parameters:R name
時間を取得するファイル名 SysTime fileCreationTime
ファイルの作成時刻 SysTime fileAccessTime
ファイルが最後にアクセスされた時刻 SysTime fileModificationTime
ファイルが最後に変更された時刻 Throws:FileExceptionエラー時- void
setTimes
(R)(Rname
, SysTimeaccessTime
, SysTimemodificationTime
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
voidsetTimes
(R)(auto ref Rname
, SysTimeaccessTime
, SysTimemodificationTime
)
if (isConvertibleToString!R); - ファイルやフォルダーのアクセス/変更時刻を設定する
name
.Parameters:R name
時間を取得するファイル/フォルダ名 SysTime accessTime
ファイル/フォルダーが最後にアクセスされた時刻。 SysTime modificationTime
ファイル/フォルダーが最後に変更された時間。 Throws:FileExceptionエラー時Examples:import std.datetime : DateTime, hnsecs, SysTime; scope(exit) deleteme.remove; write(deleteme, "a"); SysTime accessTime = SysTime(DateTime(2010, 10, 4, 0, 0, 30)); SysTime modificationTime = SysTime(DateTime(2018, 10, 4, 0, 0, 30)); setTimes(deleteme, accessTime, modificationTime); SysTime accessTimeResolved, modificationTimeResolved; getTimes(deleteme, accessTimeResolved, modificationTimeResolved); writeln(accessTime); // accessTimeResolved writeln(modificationTime); // modificationTimeResolved
- SysTime
timeLastModified
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
SysTimetimeLastModified
(R)(auto ref Rname
)
if (isConvertibleToString!R); - 指定したファイルが最後に更新された時刻を返す。Parameters:
R name
チェックするファイル名 Returns:Throws:FileException指定されたファイルが存在しない場合Examples:import std.datetime : abs, DateTime, hnsecs, SysTime; scope(exit) deleteme.remove; import std.datetime : Clock, seconds; auto currTime = Clock.currTime(); enum leeway = 5.seconds; deleteme.write("bb"); assert(abs(deleteme.timeLastModified - currTime) <= leeway);
- SysTime
timeLastModified
(R)(Rname
, SysTimereturnIfMissing
)
if (isSomeFiniteCharInputRange!R); - 与えられたファイルが最後に更新された時刻を返す。もし ファイルが存在しない場合は
returnIfMissing
.のようなビルド自動化ツールでよく使われる。 やantのようなビルド自動化ツールでよく使われる。ファイル target をファイルsource からリビルドしなければならないかどうか(すなわち、target がファイル より古いか、存在しないか)を調べるには、比較を使用する。 がsource より古いか、存在しない)。 を使う。このコードは以下をスローする。 FileExceptionsource をスローする。 を投げる(当然である)。一方、SysTime.min のデフォルト は存在しないtarget が限りなく古く見えるので、テストは正しくビルドを促す。 は正しくビルドを促す。Parameters:R name
更新時刻を取得するファイル名。 SysTime returnIfMissing
与えられたファイルが存在しない場合に返す時間。 Returns:例:
if (source.timeLastModified >= target.timeLastModified(SysTime.min)) { // (再)構築しなければならない } else { // ターゲットは最新である }
Examples:import std.datetime : SysTime; writeln("file.does.not.exist".timeLastModified(SysTime.min)); // SysTime.min auto source = deleteme ~ "source"; auto target = deleteme ~ "target"; scope(exit) source.remove, target.remove; source.write("."); assert(target.timeLastModified(SysTime.min) < source.timeLastModified); target.write("."); assert(target.timeLastModified(SysTime.min) >= source.timeLastModified);
- pure nothrow SysTime
timeLastModified
()(auto ref stat_tstatbuf
); この関数はPOSIX専用である。 与えられたファイルが最後に更新された時刻を返す。Parameters:stat_t statbuf
stat_tはファイルから取得される。 - pure nothrow SysTime
timeLastAccessed
()(auto ref stat_tstatbuf
); この関数は POSIX-Only である。 与えられたファイルが最後にアクセスされた時刻を返す。Parameters:stat_t statbuf
stat_tはファイルから取得される。 - pure nothrow SysTime
timeStatusChanged
()(auto ref stat_tstatbuf
); この関数はPOSIX専用である。 与えられたファイルが最後に変更された時刻を返す。Parameters:stat_t statbuf
stat_tはファイルから取得される。 - bool
exists
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
boolexists
(R)(auto ref Rname
)
if (isConvertibleToString!R); - 与えられたファイル(またはディレクトリ)が存在するかどうかを判定する。Parameters:
R name
ファイル名を表す文字列または文字の範囲 Returns:入力として指定されたファイル名が存在すれば真Examples:auto f = deleteme ~ "does.not.exist"; assert(!f.exists); f.write("hello"); assert(f.exists); f.remove; assert(!f.exists);
- uint
getAttributes
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
uintgetAttributes
(R)(auto ref Rname
)
if (isConvertibleToString!R); - 与えられたファイルの属性を返す。WindowsとPOSIXシステムのファイル属性は全く異なることに注意。 が返すものである。Windowsでは、これらの属性は で返される値であるのに対し、POSIXシステムでは st_mode を呼び出すことで得られるstat struct の一部である。 を呼び出すことで得られる値の一部である。 stat 関数を呼び出すことで得られる値の一部である。 POSIXシステムでは、指定されたファイルがシンボリックリンクの場合、attributesはシンボリックリンクが指すファイルの属性となる。 属性は、シンボリックリンクが指すファイルの属性である。 リンクが指すファイルの属性である。Parameters:
R name
属性を取得するファイル。 Returns:ファイルの属性をuint 。Throws:FileExceptionエラーになる。Examples:ファイルを指定して属性を取得するimport std.exception : assertThrown; auto f = deleteme ~ "file"; scope(exit) f.remove; assert(!f.exists); assertThrown!FileException(f.getAttributes); f.write("."); auto attributes = f.getAttributes; assert(!attributes.attrIsDir); assert(attributes.attrIsFile);
Examples:ディレクトリを指定して属性を取得するimport std.exception : assertThrown; auto dir = deleteme ~ "dir"; scope(exit) dir.rmdir; assert(!dir.exists); assertThrown!FileException(dir.getAttributes); dir.mkdir; auto attributes = dir.getAttributes; assert(attributes.attrIsDir); assert(!attributes.attrIsFile);
- uint
getLinkAttributes
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
uintgetLinkAttributes
(R)(auto ref Rname
)
if (isConvertibleToString!R); - 指定されたファイルがシンボリックリンクである場合、そのファイルを指すのではなく、シンボリックリンク自体の属性を返す。 の属性を返す。与えられたファイル がシンボリックリンクでない場合、この関数はgetAttributesと同じ結果を返す。 と同じ結果を返す。Windowsでは、getLinkAttributesはgetAttributesと同じである。Windowsでは、getLinkAttributesはgetAttributesと同じである。 シンボリックリンクを扱うときに、Windows用に特別なコードを書く必要がないようにするためである。 シンボリックリンクを扱うときに、Windows用に特別なコードを書く必要がない。Parameters:
R name
シンボリックリンクの属性を取得するファイル。 Returns:属性Throws:FileExceptionエラーになる。Examples:import std.exception : assertThrown; auto source = deleteme ~ "source"; auto target = deleteme ~ "target"; assert(!source.exists); assertThrown!FileException(source.getLinkAttributes); // Windowsではシンボリックリンクが使えない version (Posix) { scope(exit) source.remove, target.remove; target.write("target"); target.symlink(source); writeln(source.readText); // "target" assert(source.isSymlink); assert(source.getLinkAttributes.attrIsSymlink); }
Examples:ファイルがシンボリックリンクでない場合、getLinkAttributesはgetAttributesと同じように動作する。import std.exception : assertThrown; auto f = deleteme ~ "file"; scope(exit) f.remove; assert(!f.exists); assertThrown!FileException(f.getLinkAttributes); f.write("."); auto attributes = f.getLinkAttributes; assert(!attributes.attrIsDir); assert(attributes.attrIsFile);
Examples:ファイルがシンボリックリンクでない場合、getLinkAttributesはgetAttributesと同じように動作する。import std.exception : assertThrown; auto dir = deleteme ~ "dir"; scope(exit) dir.rmdir; assert(!dir.exists); assertThrown!FileException(dir.getLinkAttributes); dir.mkdir; auto attributes = dir.getLinkAttributes; assert(attributes.attrIsDir); assert(!attributes.attrIsFile);
- void
setAttributes
(R)(Rname
, uintattributes
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
voidsetAttributes
(R)(auto ref Rname
, uintattributes
)
if (isConvertibleToString!R); - 与えられたファイルの属性を設定する。例えば、プログラム的にはUnixの chmod +x
name
に相当するプログラムはname
.setAttributes
(name
.getAttributes | octal!700).Parameters:R name
ファイル名 uint attributes
ファイルを設定する属性 Throws:FileException指定されたファイルが存在しない場合Examples:ファイルを指定して属性を設定するimport std.exception : assertThrown; import std.conv : octal; auto f = deleteme ~ "file"; version (Posix) { scope(exit) f.remove; assert(!f.exists); assertThrown!FileException(f.setAttributes(octal!777)); f.write("."); auto attributes = f.getAttributes; assert(!attributes.attrIsDir); assert(attributes.attrIsFile); f.setAttributes(octal!777); attributes = f.getAttributes; writeln((attributes & 1023)); // octal!777 }
Examples:ディレクトリを指定して属性を設定するimport std.exception : assertThrown; import std.conv : octal; auto dir = deleteme ~ "dir"; version (Posix) { scope(exit) dir.rmdir; assert(!dir.exists); assertThrown!FileException(dir.setAttributes(octal!777)); dir.mkdir; auto attributes = dir.getAttributes; assert(attributes.attrIsDir); assert(!attributes.attrIsFile); dir.setAttributes(octal!777); attributes = dir.getAttributes; writeln((attributes & 1023)); // octal!777 }
- @property bool
isDir
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
@property boolisDir
(R)(auto ref Rname
)
if (isConvertibleToString!R); - 指定されたファイルがディレクトリであるかどうかを返す。Parameters:
R name
ファイルへのパス。 Returns:nameがディレクトリを指定した場合はtrueThrows:FileException指定されたファイルが存在しない場合Examples:import std.exception : assertThrown; auto dir = deleteme ~ "dir"; auto f = deleteme ~ "f"; scope(exit) dir.rmdir, f.remove; assert(!dir.exists); assertThrown!FileException(dir.isDir); dir.mkdir; assert(dir.isDir); f.write("."); assert(!f.isDir);
- pure nothrow @nogc @safe bool
attrIsDir
(uintattributes
); - 指定されたファイル属性がディレクトリのものかどうかを返す。Parameters:
uint attributes
ファイル属性 Returns:attributesがディレクトリを指定する場合はtrueExamples:import std.exception : assertThrown; auto dir = deleteme ~ "dir"; auto f = deleteme ~ "f"; scope(exit) dir.rmdir, f.remove; assert(!dir.exists); assertThrown!FileException(dir.getAttributes.attrIsDir); dir.mkdir; assert(dir.isDir); assert(dir.getAttributes.attrIsDir); f.write("."); assert(!f.isDir); assert(!f.getAttributes.attrIsDir);
- @property bool
isFile
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
@property boolisFile
(R)(auto ref Rname
)
if (isConvertibleToString!R); - 指定されたファイル(またはディレクトリ)がファイルであるかどうかを返す。Windowsでは、ファイルがディレクトリでなければファイルである。つまり または
isFile
またはisDir 、与えられたファイルに対して真を返す。 POSIXシステムでは、もしisFile
がtrue の場合、そのファイル が通常のファイルであることを示す(例えば、ブロック・ノット・デバイスではない)。つまり、POSIXシステム上では の両方が可能である。isFile
とisDir の両方がfalse になることもある。 の両方が可能である(この場合、それは特別なファイルである)。その場合は特別なファイルとなる。 getAttributes を使って属性を取得し、どのような特殊なファイルなのかを知ることができる。 であるか、DirEntry を使って、statBuf の結果を得ることができる。 stat どちらの場合も、stat のマニュアル・ページを参照のこと。 のマニュアルページを参照のこと。Parameters:R name
ファイルへのパス。 Returns:nameがファイルを指定した場合はtrueThrows:FileException指定されたファイルが存在しない場合Examples:import std.exception : assertThrown; auto dir = deleteme ~ "dir"; auto f = deleteme ~ "f"; scope(exit) dir.rmdir, f.remove; dir.mkdir; assert(!dir.isFile); assert(!f.exists); assertThrown!FileException(f.isFile); f.write("."); assert(f.isFile);
- pure nothrow @nogc @safe bool
attrIsFile
(uintattributes
); - 指定されたファイル属性がファイルのものかどうかを返す。Windowsでは、ファイルがディレクトリでなければファイルである。したがって
attrIsFile
true またはattrIsDir は 属性を返す。 POSIXシステムでは、もしattrIsFile
がtrue の場合 である場合、それは通常のファイルであることを示す(例えば、ブロック・ノット・デバイスではない)。つまり、POSIXシステムでは の両方が可能である。attrIsFile
とattrIsDir の両方がfalse であることがある(この場合、そのファイルは特殊なファイルである)。ファイルが ファイルが特殊ファイルである場合、属性を使ってどのような種類の特殊ファイルであるかを調べることができる( のマニュアル・ページを参照)。 をチェックすることができる(詳しくはstat のマニュアル・ページを参照)。Parameters:uint attributes
ファイルの属性である。 Returns:与えられたファイル属性がファイルのものであればtrueを返す。例:
assert(attrIsFile(getAttributes("/etc/fonts/fonts.conf"))); assert(attrIsFile(getLinkAttributes("/etc/fonts/fonts.conf")));
Examples:import std.exception : assertThrown; auto dir = deleteme ~ "dir"; auto f = deleteme ~ "f"; scope(exit) dir.rmdir, f.remove; dir.mkdir; assert(!dir.isFile); assert(!dir.getAttributes.attrIsFile); assert(!f.exists); assertThrown!FileException(f.getAttributes.attrIsFile); f.write("."); assert(f.isFile); assert(f.getAttributes.attrIsFile);
- @property bool
isSymlink
(R)(Rname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
@property boolisSymlink
(R)(auto ref Rname
)
if (isConvertibleToString!R); - 指定されたファイルがシンボリックリンクであるかどうかを返す。Windowsでは、ファイルがシンボリックリンクまたはジャンクション・ポイントである場合、true を返す。 ジャンクションポイントである場合に返す。Parameters:
R name
ファイルへのパス。 Returns:名前がシンボリックリンクの場合は真Throws:FileException指定されたファイルが存在しない場合Examples:import std.exception : assertThrown; auto source = deleteme ~ "source"; auto target = deleteme ~ "target"; assert(!source.exists); assertThrown!FileException(source.isSymlink); // symlinkingはWindowsでは利用できない version (Posix) { scope(exit) source.remove, target.remove; target.write("target"); target.symlink(source); writeln(source.readText); // "target" assert(source.isSymlink); assert(source.getLinkAttributes.attrIsSymlink); }
- pure nothrow @nogc @safe bool
attrIsSymlink
(uintattributes
); - 指定されたファイル属性がシンボリックリンクであるかどうかを返す。Windowsでは、ファイルがシンボリックリンクかジャンクションポイントである場合、true を返す。 ジャンクション・ポイントである場合に返す。Parameters:
uint attributes
ファイルの属性である。 Returns:属性がシンボリックリンクの場合はtrue例:」である。
core.sys.posix.unistd.symlink("/etc/fonts/fonts.conf", "/tmp/alink"); assert(!getAttributes("/tmp/alink").isSymlink); assert(getLinkAttributes("/tmp/alink").isSymlink);
Examples:import std.exception : assertThrown; auto source = deleteme ~ "source"; auto target = deleteme ~ "target"; assert(!source.exists); assertThrown!FileException(source.getLinkAttributes.attrIsSymlink); // symlinkingはWindowsでは利用できない version (Posix) { scope(exit) source.remove, target.remove; target.write("target"); target.symlink(source); writeln(source.readText); // "target" assert(source.isSymlink); assert(source.getLinkAttributes.attrIsSymlink); }
- void
chdir
(R)(Rpathname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
voidchdir
(R)(auto ref Rpathname
)
if (isConvertibleToString!R); - ディレクトリを次のように変更する。
pathname
.WindowsおよびPOSIXのcd に相当する。 に相当する。Parameters:R pathname
踏み込むディレクトリ Throws:FileExceptionエラー時にExamples:import std.algorithm.comparison : equal; import std.algorithm.sorting : sort; import std.array : array; import std.path : buildPath; auto cwd = getcwd; auto dir = deleteme ~ "dir"; dir.mkdir; scope(exit) cwd.chdir, dir.rmdirRecurse; dir.buildPath("a").write("."); dir.chdir; // ディレクトリに入る "b".write("."); assert(dirEntries(".", SpanMode.shallow).array.sort.equal( [".".buildPath("a"), ".".buildPath("b")] ));
- void
mkdir
(R)(Rpathname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
voidmkdir
(R)(auto ref Rpathname
)
if (isConvertibleToString!R); - 新しいディレクトリを作る
pathname
.Parameters:R pathname
作成するディレクトリのパス Throws:FileExceptionを作成するディレクトリのパスを指定する。 WindowsExceptionWindowsの場合は でエラーが発生した場合Examples:import std.file : mkdir; auto dir = deleteme ~ "dir"; scope(exit) dir.rmdir; dir.mkdir; assert(dir.exists);
Examples:import std.exception : assertThrown; assertThrown("a/b/c/d/e".mkdir);
- @safe void
mkdirRecurse
(scope const(char)[]pathname
); - 必要に応じて、ディレクトリとすべての親ディレクトリを作成する。で指定されたディレクトリが
pathname
で指定されたディレクトリが既に存在する場合は何もしない。Parameters:const(char)[] pathname
作成するディレクトリのフルパス Throws:FileExceptionを指定する。Examples:import std.path : buildPath; auto dir = deleteme ~ "dir"; scope(exit) dir.rmdirRecurse; dir.mkdir; assert(dir.exists); dir.mkdirRecurse; // 何もしない // 必要に応じてすべての親ディレクトリを作成する auto nested = dir.buildPath("a", "b", "c"); nested.mkdirRecurse; assert(nested.exists);
Examples:import std.exception : assertThrown; scope(exit) deleteme.remove; deleteme.write("a"); // すでにファイルなので、ディレクトリは作れない assertThrown!FileException(deleteme.mkdirRecurse);
- void
rmdir
(R)(Rpathname
)
if (isSomeFiniteCharInputRange!R && !isConvertibleToString!R);
voidrmdir
(R)(auto ref Rpathname
)
if (isConvertibleToString!R); - ディレクトリを削除する
pathname
.Parameters:R pathname
ディレクトリ名を指定する範囲または文字列 Throws:FileExceptionを指定する。Examples:auto dir = deleteme ~ "dir"; dir.mkdir; assert(dir.exists); dir.rmdir; assert(!dir.exists);
- void
symlink
(RO, RL)(ROoriginal
, RLlink
)
if ((isSomeFiniteCharInputRange!RO || isConvertibleToString!RO) && (isSomeFiniteCharInputRange!RL || isConvertibleToString!RL)); この関数はPOSIX専用である。 シンボリックリンク(symlink)を作成する。Parameters:RO original
リンク先のファイル。これは シンボリックリンクに格納される。相対パスは、作成された シンボリックリンクからの相対パスである。 RL link
作成するシンボリックリンク。相対パスは 相対パスは、現在の作業ディレクトリからの相対パスである。 Throws:FileExceptionエラー時に (シンボリックリンクがすでに存在する場合も含む) が存在する場合も含む)。- string
readLink
(R)(Rlink
)
if (isSomeFiniteCharInputRange!R || isConvertibleToString!R); この関数は POSIX-Only である。 シンボリックリンクが指すファイルへのパスを返す。注釈:」である。 パスは、シンボリックリンクによって相対パスにも絶対パスにもなることに注意すること。 相対パスの場合は、シンボリックリンクからの相対パスであり、 現在の作業ディレクトリからの相対パスではない。 ディレクトリを指す。Throws:FileExceptionエラー時に- @trusted string
getcwd
(); - 現在の作業ディレクトリを取得する。Throws:FileExceptionエラー時にExamples:
auto s = getcwd(); assert(s.length);
- @trusted string
thisExePath
(); - 現在の実行ファイルのフルパスを返す。Returns:実行ファイルのパスをstring として返す。Throws:Examples:
import std.path : isAbsolute; auto path = thisExePath(); assert(path.exists); assert(path.isAbsolute); assert(path.isFile);
- struct
DirEntry
; - POSIXシステムでstatから得られるような、ファイルに関する情報。
- @safe this(return scope string
path
); - 与えられたファイル(またはディレクトリ)に対してDirEntry を構築する。Parameters:
string path
DirEntry を取得するファイル (またはディレクトリ)。 Throws:FileExceptionファイルが存在しない場合 - const @property @safe string
name
() return; - このDirEntry で表されるファイルへのパスを返す。
例:
auto de1 = DirEntry("/etc/fonts/fonts.conf"); assert(de1.name == "/etc/fonts/fonts.conf"); auto de2 = DirEntry("/usr/share/include"); assert(de2.name == "/usr/share/include");
- @property @safe bool
isDir
(); - このDirEntry で表されるファイルが ディレクトリであるかどうかを返す。
例:
auto de1 = DirEntry("/etc/fonts/fonts.conf"); assert(!de1.isDir); auto de2 = DirEntry("/usr/share/include"); assert(de2.isDir);
- @property @safe bool
isFile
(); - このDirEntry で表されるファイルがファイルであるかどうかを返す。Windowsでは、ファイルがディレクトリでなければファイルである。つまり または
isFile
またはisDir はtrue を返す。 POSIXシステムでは、もしisFile
がtrue の場合は である場合、そのファイルは通常のファイルであることを示す(例えば、ブロック・ノット・デバイスではない)。つまり POSIXシステムでは、"@system"とisFile
とisDir の両方が可能である。 false の両方が可能である(この場合、それは特別なファイルである)。 ファイルである)。attributes 、またはstatBuf 。 を使用することができる。 を参照のこと)。例:
auto de1 = DirEntry("/etc/fonts/fonts.conf"); assert(de1.isFile); auto de2 = DirEntry("/usr/share/include"); assert(!de2.isFile);
- @property @safe bool
isSymlink
(); - このDirEntry で表されるファイルがシンボリックリンクであるかどうかを返す。 シンボリックリンクであるかどうかを返す。Windowsでは、ファイルがシンボリックリンクかジャンクション・ポイントである場合、true を返す。 リンクまたはジャンクション・ポイントである場合に を返す。
- @property @safe ulong
size
(); - で表されるファイルのサイズをバイト数で返す。DirEntry で表されるファイルのサイズをバイト数で返す。
- const @property @safe SysTime
timeCreated
(); この関数はWindows専用である。 で表されるファイルの作成時刻を返す。 DirEntry.- @property @safe SysTime
timeLastAccessed
(); - このDirEntry で表されるファイルが最後にアクセスされた時刻を返す。 最後にアクセスされた時刻を返す。多くのファイルシステムは、ファイルのアクセス時刻を更新しないことに注意。 (のアクセス時刻を更新しない(一般的にはパフォーマンス上の理由から)。
timeLastAccessed
と同じ値を返す可能性が高い。 timeLastModified. - @property @safe SysTime
timeLastModified
(); - このDirEntry で表されるファイルが最後に更新された時刻を返す。 が最後に更新された時刻を返す。
- const @property @safe SysTime
timeStatusChanged
(); この関数はPOSIX専用である。 DirEntry この関数は POSIX-Only である。 最後に変更された時刻を返す(内容だけでなく、パーミッションや所有権も)。- @property @safe uint
attributes
(); - この関数は、このDirEntry で表されるファイルの属性を返す。WindowsとPOSIXシステムのファイル属性は全く異なることに注釈: 。 全く異なる。Windowsでは、これらの属性は GetFileAttributes によって返されるものである。 で返されるものである。一方、POSIXシステムでは、st_mode の値である。 stat を呼び出すことで得られるstat 構造体の一部である。 POSIXシステムでは、このDirEntry で表されるファイルがシンボリックリンクの場合、attributesはそのファイルの属性となる。 POSIXシステムでは、この が示すファイルがシンボリックリンクである場合、attributesはシンボリックリンクが指すファイルの属性となる。 の属性である。
- @property @safe uint
linkAttributes
(); - POSIXシステムでは、このDirEntry で表されるファイルがシンボリックリンクの場合、attributesはシンボリックリンクが指すファイルの属性となる。 シンボリックリンクである場合
linkAttributes
の属性である。 である。そうでなければlinkAttributes
と同じである。 attributes.WindowsではlinkAttributes
はattributes と同じである。 と同じである。 シンボリックリンクを扱うときに、Windows用に特別なコードを記述する必要がない。 - @property @safe stat_t
statBuf
(); この関数はPOSIX専用である。 stat を呼び出すと、stat 構造体が得られる。
- PreserveAttributes
preserveAttributesDefault
; - WindowsのデフォルトはYes.preserveAttributes で、その他のプラットフォームではその逆である。
- void
copy
(RF, RT)(RFfrom
, RTto
, PreserveAttributespreserve
= preserveAttributesDefault)
if (isSomeFiniteCharInputRange!RF && !isConvertibleToString!RF && isSomeFiniteCharInputRange!RT && !isConvertibleToString!RT);
voidcopy
(RF, RT)(auto ref RFfrom
, auto ref RTto
, PreserveAttributespreserve
= preserveAttributesDefault)
if (isConvertibleToString!RF || isConvertibleToString!RT); - ファイルをコピーする
from
をファイルにコピーする。to
.ファイルのタイムスタンプは保持される。 もしpreserve
がYes.preserveAttributes と等しい場合、ファイルの属性は保持される。 Windowsでは、Yes.preserveAttributes (Windowsのデフォルト)のみがサポートされる。 ターゲット・ファイルが存在する場合、上書きされる。Parameters:RF from
既存のファイル名を表す文字列または文字の範囲 RT to
ターゲット・ファイル名を表す文字列または文字の範囲 PreserveAttributes preserve
ファイル属性を保持するかどうか Throws:FileExceptionエラー時にExamples:auto source = deleteme ~ "source"; auto target = deleteme ~ "target"; auto targetNonExistent = deleteme ~ "target2"; scope(exit) source.remove, target.remove, targetNonExistent.remove; source.write("source"); target.write("target"); writeln(target.readText); // "target" source.copy(target); writeln(target.readText); // "source" source.copy(targetNonExistent); writeln(targetNonExistent.readText); // "source"
- @safe void
rmdirRecurse
(scope const(char)[]pathname
);
@safe voidrmdirRecurse
(ref scope DirEntryde
);
@safe voidrmdirRecurse
(scope DirEntryde
); - ディレクトリとそのすべてのコンテンツとサブディレクトリを削除する、 を再帰的に削除する。Parameters:
const(char)[] pathname
完全に削除するディレクトリのパス DirEntry de
ディレクトリの DirEntryを削除する。 Throws:FileExceptionエラーがあった場合(与えられた ファイルがディレクトリでない場合も含む)。Examples:import std.path : buildPath; auto dir = deleteme.buildPath("a", "b", "c"); dir.mkdirRecurse; assert(dir.exists); deleteme.rmdirRecurse; assert(!dir.exists); assert(!deleteme.exists);
- enum
SpanMode
: int; - dirEntriesのディレクトリスパニングポリシーを指示する(下記参照)。Examples:
import std.algorithm.comparison : equal; import std.algorithm.iteration : map; import std.algorithm.sorting : sort; import std.array : array; import std.path : buildPath, relativePath; auto root = deleteme ~ "root"; scope(exit) root.rmdirRecurse; root.mkdir; root.buildPath("animals").mkdir; root.buildPath("animals", "cat").mkdir; alias removeRoot = (return scope e) => e.relativePath(root); assert(root.dirEntries(SpanMode.depth).map!removeRoot.equal( [buildPath("animals", "cat"), "animals"])); assert(root.dirEntries(SpanMode.breadth).map!removeRoot.equal( ["animals", buildPath("animals", "cat")])); root.buildPath("plants").mkdir; assert(root.dirEntries(SpanMode.shallow).array.sort.map!removeRoot.equal( ["animals", "plants"]));
shallow
- 1つのディレクトリにのみスパンする。
depth
breadth
- 注釈: この場合 SpanMode.
breadth
はすべてのディレクトリ のメンバがサブディレクトリのメンバより前になることはない。 真の つまり、真の幅優先走査ではない。
- auto
dirEntries
(bool useDIP1000 = dip1000Enabled)(stringpath
, SpanModemode
, boolfollowSymlink
= true);
autodirEntries
(bool useDIP1000 = dip1000Enabled)(stringpath
, stringpattern
, SpanModemode
, boolfollowSymlink
= true); - 入力範囲 DirEntry の入力範囲を返す、 また、2通りのforeach反復を提供する。イテレーション変数は string 。DirEntry である。スパン・モードは ディレクトリがどのように走査されるかを決定する。反復される各ディレクトリエントリの名前 の名前には、絶対パスか相対パスが含まれる(パス名による)。
注釈 返されるディレクトリエントリの順序は、オペレーティングシステム/ ファイルシステムによって提供されるものである。 オペレーティングシステム/ファイルシステムによって提供されるものであり、 特定のソートに従っているわけではない。
Parameters:useDIP1000 この関数は、-preview=dip1000コンパイラ・スイッチのあるコードとないコードで別々にインスタンス化するために使用される。 と-preview=dip1000コンパイラ・スイッチのないコードでは、この関数を別々にインスタンス化する必要がある。 この関数のABIに影響するからである。自動的に設定される。 触らない。 string path
反復処理するディレクトリ。 空の場合、カレントディレクトリが反復される。 string pattern
.d "のようなワイルドカードを持つオプションの文字列。この文字列がある場合 の結果をファイル名で絞り込むために使われる。サポートされるワイルドカード 文字列については std.path.globMatch. SpanMode mode
ディレクトリのサブディレクトリを にするかどうか。depth), 深さ優先プレオーダー (breadth)、あるいはまったく (shallow). bool followSymlink
ディレクトリを指すシンボリックリンクを ディレクトリを指すシンボリックリンクをディレクトリとして扱い を反復処理するかどうか。 Throws:- FileExceptionpath ディレクトリが存在しないか、読み取りパーミッションが拒否されている場合。
- FileExceptionmode がshallow でなく、サブディレクトリが読めない場合。
例:
// ディレクトリを深く反復する foreach (string name; dirEntries("destroy/me", SpanMode.depth)) { remove(name); } // 現在のディレクトリを反復処理する foreach (string name; dirEntries("", SpanMode.breadth)) { writeln(name); } // ディレクトリを反復処理し、その詳細情報を取得する foreach (DirEntry e; dirEntries("dmd-testing", SpanMode.breadth)) { writeln(e.name, "\t", e.size); } // 現在のディレクトリとそのすべてのサブディレクトリにあるすべての*.dファイルを反復処理する auto dFiles = dirEntries("", SpanMode.depth).filter!(f => f.name.endsWith(".d")); foreach (d; dFiles) writeln(d.name); // std.parallelismとフックして、それらすべてを並列にコンパイルする: foreach (d; parallel(dFiles, 1)) //各スレッドに1ファイルずつ渡す { string cmd = "dmd -c " ~ d.name; writeln(cmd); std.process.executeShell(cmd); } // Iterate over all D source files in current directory and all its // subdirectories auto dFiles = dirEntries("","*.{d,di}",SpanMode.depth); foreach (d; dFiles) writeln(d.name);
読み取りパーミッションが拒否されたサブディレクトリを扱うには、SpanMode.shallow を使う:void scan(string path) { foreach (DirEntry entry; dirEntries(path, SpanMode.shallow)) { try { writeln(entry.name); if (entry.isDir) scan(entry.name); } catch (FileException fe) { continue; } // 無視する } } scan("");
Examples:D1のstd.file.listdir() :string[] listdir(string pathname) { import std.algorithm.iteration : map, filter; import std.array : array; import std.path : baseName; return dirEntries(pathname, SpanMode.shallow) .filter!(a => a.isFile) .map!((return a) => baseName(a.name)) .array; } // preview=dip1000でのみ安全である @safe void main(string[] args) { import std.stdio : writefln; string[] files = listdir(args[1]); writefln("%s", files); }
- Select!(Types.length == 1, Types[0][], Tuple!Types[])
slurp
(Types...)(stringfilename
, scope const(char)[]format
); - ファイルを1行ずつ読み、その行を1つの値または std.typecons.TupleTypes の長さに応じて、単一の値または複数の値に解析される。 行は、指定されたフォーマット文字列を使って解析される。フォーマット文字列は に渡される。 std.format.formattedReadに渡されるので で説明されている書式文字列仕様に準拠しなければならない。 std.format.Parameters:
Types 行の各要素が返すべき型は以下の通りである。 string filename
読み込むファイル名 const(char)[] format
読み込み時に使用するフォーマット文字列 Returns:型が1つしか渡されない場合は、その型の配列となる。それ以外の場合は の配列となる。 std.typecons.Tuples.Throws:Exception をスローする。また、次のようにスローされる。 をスローする。 をスローする。Exceptionstd.format.formattedRead.つまり、空行や余分な文字を含む 余分な文字を含む行は許されない。Examples:import std.typecons : tuple; scope(exit) { assert(exists(deleteme)); remove(deleteme); } write(deleteme, "12 12.25\n345 1.125"); // deleteemeは一時ファイルの名前である // ファイル読込み; 各行はintの後にカンマ、空白、doubleが // 続く。 auto a = slurp!(int, double)(deleteme, "%s %s"); writeln(a.length); // 2 writeln(a[0]); // tuple(12, 12.25) writeln(a[1]); // tuple(345, 1.125)
- @trusted string
tempDir
(); - 一時ファイル用ディレクトリへのパスを返す。 POSIXプラットフォームでは、次のようなディレクトリのリストを検索して を検索し、最初に存在したものを返す:
- 環境変数TMPDIR で指定されたディレクトリ。
- 環境変数TEMP で指定されたディレクトリ。
- 環境変数TMP で指定されたディレクトリ。
- /tmp/
- /var/tmp/
- /usr/tmp/
すべてのプラットフォームで使用できる、tempDir
は失敗すると現在の作業ディレクトリを返す。 この関数の戻り値はキャッシュされるので、以下に説明する手続きは、この関数が最初に呼ばれたときにのみ実行される。 関数の戻り値はキャッシュされるので、以下に説明する手続きは、この関数が最初に呼ばれたときにのみ実行される。 それ以降 それ以降の実行は、環境変数やディレクトリ構造が 環境変数やディレクトリ構造がその間に変更されたかどうかに関係なく、それ以降の実行はすべて同じ文字列を返す。 を返す。 POSIXのtempDir
アルゴリズムはPythonの tempfile.tempdir.Returns:Windowsでは、この関数はWindows API関数を呼び出した結果を返す。 GetTempPath. POSIXプラットフォームでは、この関数は以下のディレクトリのリストを検索し、存在することがわかった最初のディレクトリを返す。 を検索し、最初に存在するものを返す:- 環境変数TMPDIR で指定されたディレクトリ。
- 環境変数TEMP で指定されたディレクトリ。
- 環境変数TMP で指定されたディレクトリ。
- /tmp
- /var/tmp
- /usr/tmp
tempDir
は失敗すると"." を返す。 を返す。Examples:import std.ascii : letters; import std.conv : to; import std.path : buildPath; import std.random : randomSample; import std.utf : byCodeUnit; // 20文字のランダムID auto id = letters.byCodeUnit.randomSample(20).to!string; auto myFile = tempDir.buildPath(id ~ "my_tmp_file"); scope(exit) myFile.remove; myFile.write("hello"); writeln(myFile.readText); // "hello"
- @safe ulong
getAvailableDiskSpace
(scope const(char)[]path
); - 指定されたパスに基づく利用可能なディスク容量を返す。 Windowsでは
path
はディレクトリでなければならないが、POSIXシステムではファイルでもディレクトリでもよい。Parameters:const(char)[] path
Windowsではディレクトリでなければならないが、POSIXシステムではファイルでもディレクトリでもよい。 Returns:利用可能なバイト数Throws:FileException故障の場合Examples:import std.exception : assertThrown; auto space = getAvailableDiskSpace("."); assert(space > 0); assertThrown!FileException(getAvailableDiskSpace("ThisFileDoesNotExist123123"));
Copyright © 1999-2024 by the D Language Foundation
DEEPL APIにより翻訳、ところどころ修正。
このページの最新版(英語)
このページの原文(英語)
翻訳時のdmdのバージョン: 2.108.0
ドキュメントのdmdのバージョン: 2.109.1
翻訳日付 :
HTML生成日時:
編集者: dokutoku