5

node.js は Windows 上のファイル属性を取得および変更する方法を提供しないため、子プロセスを実行する必要があります。すべてのファイル属性を取得したい、つまり:

  • サイズ
  • 記録
  • 隠れた
  • 読み取り専用
  • システム
  • 作成/変更/アクセス時間
  • ファイル?/ディレクトリ?/シンボリックリンク? (ジャンクション)

子プロセスを実行する場合、fs.stat を呼び出したくありません。これは追加の I/O アクセスになるためです (また、Stats は Windows に関する情報をあまり提供しません)。子プロセスを実行すると、すべてのデータを一度に取得したいと考えています。

だから、私はpowershellスクリプトを書いた:

var cmd = "powershell -Command \"$item=get-item a -force;[bool]($item.attributes -band [io.fileattributes]::directory);[bool]($item.attributes -band [io.fileattributes]::archive);[bool]($item.attributes -band [io.fileattributes]::reparsepoint);[bool]($item.attributes -band [io.fileattributes]::hidden);[bool]($item.attributes -band [io.fileattributes]::readonly);[bool]($item.attributes -band [io.fileattributes]::system);$item.length;$tmp=$item.creationtime;$tmp.year;$tmp.month;$tmp.day;$tmp.hour;$tmp.minute;$tmp.second;$tmp.millisecond;$tmp=$item.lastaccesstime;$tmp.year;$tmp.month;$tmp.day;$tmp.hour;$tmp.minute;$tmp.second;$tmp.millisecond;$tmp=$item.lastwritetime;$tmp.year;$tmp.month;$tmp.day;$tmp.hour;$tmp.minute;$tmp.second;$tmp.millisecond;$s\"";

これは次を返します: (一度 javascript で分割されました: split("\r\n"))

[ 'False', //directory?
  'True', //archive?
  'False', //symlink?
  'False', //hidden
  'False', //readonly?
  'False', //system?
  '3', //length (if directory, empty string)
  '2012', //creation time, year
  '11', //creation time, month
  '18', //creation time, day
  '6', //creation time, hour
  '8', //creation time, min
  '44', //creation time, ysec
  '457', //creation time, millis
  '2012', //last access time, year...
  '11',
  '18',
  '6',
  '8',
  '44',
  '457',
  '2012', //last modified time, year...
  '11',
  '18',
  '14',
  '0',
  '38',
  '859',
  '' ]

問題は、Windows XP には powershell が付属しておらず、それをインストールする必要があることです (ところで、最近 node.js で Windows XP を使用している人はいますか? ばかげています)、同じ情報を取得できる cmd コマンドを探しています。必要なものはすべて表示されますdirが、秒とミリ秒は表示されず、ファイルがシンボリックリンクであるかどうかを判断する方法が見つかりませんでした...


編集: 解決策は Windows Script Host にあるようです。Windows 98 以降で利用可能で、スクリプトは JavaScript で記述されています。

解決:

jscript の Windows ホスト スクリプト:

wh.js

var fs = new ActiveXObject ("Scripting.FileSystemObject");
var name = WScript.Arguments.item (0);
var file;
try{
    file = fs.getFile (name);
}catch (e){
    file = fs.getFolder (name);
}
//http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117%28v=vs.85%29
//-1 if true, 0 if false
WScript.echo (!!(file.attributes & 1)); //Read-only
WScript.echo (!!(file.attributes & 2)); //Hidden
WScript.echo (!!(file.attributes & 4)); //System
WScript.echo (!!(file.attributes & 16)); //Directory
WScript.echo (!!(file.attributes & 32)); //Archive
WScript.echo (!!(file.attributes & 1024)); //Reparse point (symbolic link)
WScript.echo (file.size); //0 if directory
WScript.echo (file.dateCreated);
WScript.echo (file.dateLastAccessed);
WScript.echo (file.dateLastModified);

Node.js:

var file = "a";
require ("child_process").exec ("cscript " + __dirname +
        "/wsh.js " + file + " //Nologo",
        function (error, stdout, stderr){
            if (error) return console.log (error);
            if (stderr) return console.log (stderr);
            stdout = stdout.split ("\r\n");
            console.log(stdout)
});

結果:

[ '0',
  '0',
  '0',
  '0',
  '-1',
  '0',
  '3',
  '18/11/2012 15:45:04',
  '18/11/2012 15:45:04',
  '18/11/2012 15:45:12',
  '' ]

ミリ秒は取得できませんが、問題ありません (Linux の atime、mtime には ms がありません)。

4

1 に答える 1

3

あなたはすでに自分自身に答えました.しかし、バッチファイルでいくつかのファイル属性を読み取る方法があります :

c:\>for /f %A in ("example.file") do echo %~aA

のバッチ スクリプトで:

for /f %%A in ("example.file") do echo %%~aA

または ATTRIB を使用:

c:\>attrib + semitest.bat /s /d

属性は次のとおりです。

    R  Read-only (1)
    H  Hidden (2)
    A  Archive (32)
    S  System (4)

拡張属性:

    E  Encrypted
    C  Compressed (128:read-only)
    I  Not content-indexed
    L  Symbolic link/Junction (64:read-only)
    N  Normal (0: cannot be used for file selection)
    O  Offline
    P  Sparse file
    T  Temporary 
于 2012-11-22T09:54:02.647 に答える