2012-11-18 43 views
5

Vì node.js không cung cấp cách lấy và sửa đổi thuộc tính tệp trên cửa sổ, tôi cần thực thi quy trình con. Tôi muốn có được tất cả các thuộc tính tập tin, đó là:Lấy thuộc tính tệp từ cửa sổ cmd

  • kích thước
  • lưu trữ
  • ẩn
  • readonly
  • hệ thống
  • tạo/sửa đổi/truy cập thời gian
  • tập tin /? directory?/symlink? (đường giao nhau)

Nếu tôi thực hiện quy trình con Tôi không muốn gọi đến fs.stat vì đó là truy cập I/O phụ (và Số liệu thống kê không cung cấp quá nhiều thông tin trên cửa sổ). Nếu tôi thực hiện một tiến trình con, tôi muốn lấy tất cả dữ liệu cùng một lúc.

Vì vậy, tôi đã viết một kịch bản 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\""; 

này trả về: (một lần đã được tách ra trong 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', 
    '' ] 

Vấn đề là Windows XP không đi kèm với PowerShell và bạn cần phải cài đặt nó (btw người đang sử dụng Windows XP với node.js ngày nay? ngớ ngẩn), vì vậy tôi đang tìm kiếm một lệnh cmd có thể lấy thông tin tương tự. I have seen that dir có thể hiển thị tất cả những gì cần nhưng nó không hiển thị giây và mili giây và tôi đã không tìm thấy một cách để xác định xem một tập tin là một liên kết tượng trưng ...


EDIT: Giải pháp dường như nằm trong Windows Script Host. Có sẵn từ cửa sổ 98 và các tập lệnh được viết bằng javascript.

SOLUTION:

Windows Host Script trong 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) 
}); 

Kết quả:

[ '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', 
    '' ] 

mili giây không thể được lấy ra nhưng đó là ok (linux atime, mtime không có ms)

+0

"Vấn đề ở đây là Windows XP không có powerhell và bạn cần cài đặt" vì vậy đó chỉ là vấn đề bạn muốn * triển khai * điều này cho nhiều máy có phụ thuộc bổ sung vấn đề. Điều này có vẻ là một mối quan tâm kỳ lạ. Chỉ cần chỉ định PowerShell là một yêu cầu. – Richard

+1

Nhưng điều đó quá dễ dàng. Tôi thích thử thách. Câu hỏi được chỉnh sửa với một cscript. –

+0

Ít phụ thuộc hơn luôn luôn là một điều tốt. Nhờ giải pháp của bạn, tôi đã tạo ra một mô-đun nhận và đặt thuộc tính tệp/dir: https://www.npmjs.org/package/winattr –

Trả lời

2

Bạn đã trả lời cho mình.Nhưng có một cách để đọc một số tập tin attributes with a batch file:

c:\>for /f %A in ("example.file") do echo %~aA 

của trong một kịch bản hàng loạt:

for /f %%A in ("example.file") do echo %%~aA 

hoặc ATTRIB:

c:\>attrib + semitest.bat /s /d 

nơi atributes là:

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

attribu mở rộng 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 
Các vấn đề liên quan