从Windows cmd检索文件属性 [英] Retrieve file attributes from windows cmd

查看:780
本文介绍了从Windows cmd检索文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为node.js不提供检索和修改Windows上的文件属性的方法,我需要执行一个子进程。我想获取所有文件属性,即:

Because node.js doesn't offer a way to retrieve and modify the file attributes on windows I need to execute a child process. I want to get all the file attributes, that is:


  • 大小

  • >
  • 隐藏

  • readonly

  • 系统


  • 文件?/ directory?/ symlink? (junction)

  • size
  • archive
  • hidden
  • readonly
  • system
  • creation/modified/access time
  • file?/directory?/symlink? (junction)

如果我要执行一个子进程,我不想调用fs.stat,因为它是一个额外的I / O访问(和统计不提供太多的窗口信息)。如果我执行一个子进程,我想一次检索所有的数据。

If I'm going to execute a child process I don't want to call to fs.stat because it's an extra I/O access (and Stats doesn't offer too many information on windows). If I execute a child process I want to retrieve all the data at once.

所以,我写了一个powershell脚本:

So, I've written a powershell script:

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 \\\

This returns: (once has been splitted in 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,安装它(btw谁使用windows xp与node.js nowadays?silly),所以我正在搜索一个cmd命令,可以检索相同的信息。我看到 dir 可以显示所有我需要,但它不显示秒和毫秒,我没有找到一种方法来确定一个文件是一个符号链接。 ..

The problem is that windows XP doesn't come with powershell and you need to install it (btw who's using windows xp with node.js nowadays? silly), so I'm searching a cmd command that can retrieve the same information. I've seen that dir can show all I need but it doesn't show seconds and milliseconds and I've not found a way to determine if a file is a symlink...

编辑:解决方案似乎在Windows Script Host中。

The solution seems to be in Windows Script Host. Available since windows 98 and the scripts are written in javascript.

解决方案:

Windows主机脚本在jscript中:

Windows Host Script in jscript:

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)

Milliseconds cannot be retrieved but it's ok (linux atime, mtime don't have ms)

推荐答案

你已经回答了自己。某些文件带有批处理文件的属性

You already answered yourself.But there's a way to read some file 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 

这篇关于从Windows cmd检索文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆