You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取MSI文件的注释等文件信息?fs模块fs.stats无法获取

Hey there! You’re totally right that Node.js’s fs.stat() only gives you basic filesystem stats—things like file size, creation time, or permissions. It doesn’t touch Windows-specific metadata like the comment field on an MSI file. Let’s walk through two solid ways to get that info, depending on whether you want the file system-level comment or the internal MSI package comment.

1. Getting the Windows File System Comment (for any file, including MSI)

This is the comment you see when you right-click a file, go to Properties → Details → Comments. To fetch this, you can use Windows’ built-in wmic command via Node.js’s child_process module (no extra npm packages needed):

const { exec } = require('child_process');

const getWindowsFileComment = (filePath) => {
  // Escape backslashes for the wmic command (Windows requires double backslashes)
  const escapedPath = filePath.replace(/\\/g, '\\\\');
  const command = `wmic datafile where name="${escapedPath}" get comment /value`;
  
  return new Promise((resolve, reject) => {
    exec(command, (error, stdout, stderr) => {
      if (error) {
        reject(new Error(`Failed to fetch comment: ${stderr || error.message}`));
        return;
      }
      // Parse the output (format: "Comment=Your comment text")
      const commentMatch = stdout.trim().match(/Comment=(.*)/);
      resolve(commentMatch ? commentMatch[1].trim() : 'No comment found on file system');
    });
  });
};

// Example usage
getWindowsFileComment('C:\\Downloads\\MyInstaller.msi')
  .then(comment => console.log('File System Comment:', comment))
  .catch(err => console.error('Error:', err));
2. Getting the Internal MSI Package Comment

MSI files are actually databases, so they store their own internal properties (like Comments, ProductVersion, etc.) separate from the file system metadata. For this, using a dedicated MSI parsing library is the most reliable approach.

First, install the msi npm package:

npm install msi

Then use this code to extract the internal comment:

const msi = require('msi');

const getMsiInternalComment = async (msiPath) => {
  try {
    const db = await msi.open(msiPath);
    const msiProperties = await db.getProperties();
    await db.close();
    return msiProperties.Comments || 'No internal comment found in MSI';
  } catch (err) {
    throw new Error(`Failed to read MSI properties: ${err.message}`);
  }
};

// Example usage
getMsiInternalComment('C:\\Downloads\\MyInstaller.msi')
  .then(comment => console.log('MSI Internal Comment:', comment))
  .catch(err => console.error('Error:', err));

Quick Notes

  • If you don’t want to use npm packages, you can also use Windows’ built-in WiRunSQL.vbs script to query the MSI database directly via command line—just run cscript WiRunSQL.vbs "C:\Path\To\file.msi" "SELECT Value FROM Property WHERE Property='Comments'".
  • For the wmic method, make sure your file path doesn’t have quotes or special characters (if it does, you’ll need extra escaping).
  • The msi library needs Node.js 12+ and may require build tools (like Python) installed on Windows if you run into installation errors.

内容的提问来源于stack exchange,提问作者sirzento

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:38:13