UNPKG

fswin32

Version:

The ultimate Node.js module for detailed Windows file system access.

44 lines (38 loc) 1.28 kB
// src/files.js import { promises as fs } from 'fs'; import path from 'path'; import os from 'os'; import { executePowerShellCommand, formatBytes } from './utils.js'; /** * Gets detailed information about a specific file. * @param {string} filePath The absolute path of the file. * @returns {Promise<Object|null>} An object with detailed information. */ export const getFileDetails = async (filePath) => { try { const stats = await fs.stat(filePath); if (!stats.isFile()) { return null; // Not a file } let owner = 'N/A'; const isWindows = os.platform() === 'win32'; if (isWindows) { const stdout = await executePowerShellCommand(`(Get-Item -Path "${filePath}").GetAccessControl().Owner`); if (stdout) { owner = stdout.trim(); } } return { path: filePath, name: path.basename(filePath), is_file: true, last_modified: stats.mtime, size_bytes: stats.size, size_formatted: formatBytes(stats.size), owner: owner, }; } catch (error) { console.error(`Error getting details for ${filePath}:`, error.message); return null; } };