因为node.js中不提供一种方法来检索和修改Windows中的文件属性,我需要执行一个子进程。 我想所有的文件属性,那就是:
- 尺寸
- 档案
- 隐
- 只读
- 系统
- 创建/修改/访问时间
- 文件?/目录?/符号链接? (结)
如果我要去执行一个子进程,我不想打电话fs.stat,因为它是一个额外的I / O访问(和统计不提供在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,你需要安装它(顺便说一句谁在使用Windows XP时的node.js越来越大呢?傻了),所以我在寻找一个cmd命令可以获取同样的信息。 我已经看到了dir
可以显示所有我需要的,但它并没有显示秒和毫秒,我还没有找到一种方法来确定一个文件是一个符号链接...
编辑:该解决方案似乎是在Windows脚本宿主。 因为Windows 98和脚本可被用JavaScript编写的。
解:
在JScript Windows主机脚本:
whs.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,修改时间没有毫秒)