なんだこりゃ・・・
ITproのフォルダやサブフォルダを操作する(Directoryクラス) | 日経 xTECH(クロステック)をD風に書こうと思ったけど、なんだか無理っぽい。
お題:http://itpro.nikkeibp.co.jp/article/COLUMN/20070205/260746/?SS=tech_codezone&FD=
import std.cstream; import std.string; import std.path, std.file; //extern (C) ulong GetLogicalDriveStringsW(ulong, ubyte[]); int main(char[][] args) { char[] str; /// ディレクトリを作成する /* /// try { ... } catch { ... }はExceptionだとそのまま次のブロックに移るじゃないのかよヽ(`Д´)ノ try { std.file.mkdir(r"C:\tmp\abc"); } catch (FileException e) { dout.writeLine("指定したパスが無効です: " ~ e.msg); } /// ディレクトリを削除する std.file.rmdir(r"C:\tmp\abc"); try { std.file.rmdir(r"C:\tmp\abc"); } catch (FileException e) { derr.writeLine("..."); } */ /// ディレクトリが存在するかどうか調べる /// ここら辺に来ると、CPU100%のまま音沙汰が無い・・・ if (std.file.exists(r"C:\windows")) dout.writeLine("フォルダは存在します"); else derr.writeLine("フォルダは存在しません"); /// Windowsアクセス制御リスト(Access Control List = ACL)を取得する /// 多分、こうじゃ無い。 uint acl = std.file.getAttributes(r"C:\Windows"); /// 作成日時を取得する std.file.listdir(r"C:\Windows", delegate bool(DirEntry *dirs) { dout.writeLine("Create Time: " ~ std.string.toString(dirs.creationTime)); return false; }); /// 現在の作業ディレクトリを取得する str = std.file.getcwd(); dout.writeLine(str); /// サブディレクトリを列挙する /// 初めてデリゲートのありがたみが分かった瞬間w std.file.listdir(r"C:\Windows", delegate bool(DirEntry *dirs) { if (dirs.isdir()) dout.writeLine(dirs.name); return true; }); /// サブディレクトリのサブディレクトリまですべて列挙する std.file.listdir(r"C:\Windows", delegate bool(DirEntry *dirs) { std.file.listdir(dirs.name, delegate bool(DirEntry *subdirs) { if (subdirs.isdir()) dout.writeLine(dirs.name); return true; }); return true; }); /// ルート・ディクレトリを取得する (Windowsの場合はドライブか) str = std.path.getDrive(r"C:\Windows"); /// ディレクトリ内のファイル名を列挙する std.file.listdir(r"C:\Windows", delegate bool(DirEntry *dirs) { if (dirs.isfile()) dout.writeLine(dirs.name); return true; }); /// ディレクトリ内のファイル名をサブディレクトリ内ますべて列挙する std.file.listdir(r"C:\Windows", delegate bool(DirEntry *dirs) { std.file.listdir(dirs.name, delegate bool(DirEntry *subdirs) { if (subdirs.isfile()) dout.writeLine(dirs.name); return true; }); return true; }); /// 指定したディレクトリ内のすべてのファイルとすべてのサブディレクトリを列挙する str = r"C:\Program Files"; std.file.listdir(str, delegate bool(char[] filename) { dout.writeLine(std.path.join(str, filename)); return true; }); /// 最後にアクセスした日付と時刻を取得する std.file.listdir(r"C:\Windows", delegate bool(DirEntry *dirs) { dout.writeLine("Last Time: " ~ std.string.toString(dirs.lastAccessTime)); return false; }); /// 最後に書き込んだ日付と時刻を取得する std.file.listdir(r"C:\Windows", delegate bool(DirEntry *dirs) { dout.writeLine("Modify Time: " ~ std.string.toString(dirs.lastWriteTime)); return false; }); /// 論理ドライブ名を列挙する /// ここら辺は多分違う。 /// 何故かリンクできない。 /* try { ubyte buf[256]; ulong ret = GetLogicalDriveStringsW(256, buf); if (ret > 256) throw new Error("Win32API Error: GetLogicalDriveStrings: " ~ std.string.toString(ret)); char[][] drivelist = std.string.split(cast(char[])buf, null); foreach (char[] one; drivelist) dout.writeLine(one); } catch (Error e) { derr.writeLine(e.msg); } */ /// 指定したパスの親ディレクトリを取得する str = std.path.getDirName(r"C:\windows"); dout.writeLine(str); /// ディレクトリの内容をすべて移動する /// moveDir()とかが実装してないっぽい /// TODO: 後で書かない return 0; }
さらに・・・
"ディレクトリが空ではありません"が、
directorysample.d(25): found '$' when expecting ',' directorysample.d(25): found '$' when expecting ',' directorysample.d(25): expression expected, not '^' directorysample.d(25): found '$' when expecting ',' directorysample.d(25): expression expected, not ';' directorysample.d(25): found '$' when expecting ',' directorysample.d(25): unsupported char 0x1b directorysample.d(25): found ':' when expecting ',' directorysample.d(30): found 'C' when expecting ',' directorysample.d(30): expression expected, not ':' directorysample.d(30): undefined escape sequence \w directorysample.d(30): found '"w"' when expecting ',' directorysample.d(31): found '"))\x0a dout.writeLine("' when expecting ', ' directorysample.d(31): unsupported char 0x1b directorysample.d(31): '$' is valid only inside [] of index or slice directorysample.d(31): found 'B' when expecting ',' directorysample.d(31): expression expected, not '%' directorysample.d(31): found 'U' when expecting ',' directorysample.d(31): expression expected, not '%' directorysample.d(31): unsupported char '@' directorysample.d(31): '$' is valid only inside [] of index or slice
・・・orz、まぁエスケープシーケンス("\")なんだろうけど。でも、何故か"空"を"Kara"にしてもダメだった。
追記:ん?あれ?違う・・・?
import std.stdio; void main() { char[] s = "空"; foreach (char c; s) writefln("0x%02x :: %s", c, c); writefln("%s", s); return; }
う〜ん・・・$とか、それっぽい文字があるなぁ〜・・・