因为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脚本宿主中。从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,mtime没有ms)
答案 0 :(得分:2)
你已经回答了自己。但是有一种方法可以阅读一些文件attributes with a batch file:
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