Gab*_*mas 5 windows powershell cmd node.js
因为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\"";
Run Code Online (Sandbox Code Playgroud)
这将返回:(一旦在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',
'' ]
Run Code Online (Sandbox Code Playgroud)
问题是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);
Run Code Online (Sandbox Code Playgroud)
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)
});
Run Code Online (Sandbox Code Playgroud)
结果:
[ '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',
'' ]
Run Code Online (Sandbox Code Playgroud)
无法检索毫秒但是没关系(linux atime,mtime没有ms)
您已经回答了自己。但是有一种方法可以使用批处理文件读取一些文件属性 :
c:\>for /f %A in ("example.file") do echo %~aA
Run Code Online (Sandbox Code Playgroud)
在批处理脚本中:
for /f %%A in ("example.file") do echo %%~aA
Run Code Online (Sandbox Code Playgroud)
或使用 ATTRIB:
c:\>attrib + semitest.bat /s /d
Run Code Online (Sandbox Code Playgroud)
其中属性是:
R Read-only (1)
H Hidden (2)
A Archive (32)
S System (4)
Run Code Online (Sandbox Code Playgroud)
扩展属性:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1892 次 |
| 最近记录: |