2012-11-18 43 views
5

因爲node.js沒有提供在Windows上檢索和修改文件屬性的方法,所以我需要執行子進程。我想所有的文件屬性,那就是:從Windows cmd檢索文件屬性

  • 大小
  • 歸檔
  • 隱藏
  • 只讀
  • 系統
  • 創建/修改/存取時間
  • 文件/?目錄?/符號鏈接? (junction)

如果我要執行一個子進程,我不想調用fs.stat,因爲它是一個額外的I/O訪問(並且Stats不提供太多的窗口信息)。如果我執行一個子進程,我想一次檢索所有的數據。

所以,我寫了一個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 Script Host中。自Windows 98和腳本用JavaScript編寫以來可用。

SOLUTION:

Windows主機腳本在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,修改時間沒有毫秒)

+0

「問題是,Windows XP不來使用PowerShell,你需要安裝」所以這不僅是要*部署*這多臺機器,其中一個額外的依賴將是一個問題一個問題。這似乎是一個奇怪的問題。只需將PowerShell指定爲要求即可。 – Richard

+1

但這太容易了。我喜歡挑戰。問題是用cscript編輯的。 –

+0

更少的依賴性總是一件好事。感謝您的解決方案,我創建了一個獲取並設置文件/目錄屬性的模塊:https://www.npmjs.org/package/winattr –

回答

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 

其中atributes是:

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

extended attribu TES:

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