怎么用脚本获取文件属性摘要

wen 实用脚本 3

本文目录导读:

怎么用脚本获取文件属性摘要

  1. Windows PowerShell 脚本
  2. Python 脚本
  3. 使用 Python + win32com 获取Windows扩展属性
  4. Linux/macOS Shell 脚本
  5. 批量处理脚本(PowerShell)
  6. Node.js 脚本
  7. 使用方法:

Windows PowerShell 脚本

# 获取单个文件属性
$file = Get-Item "C:\path\to\your\file.txt"
$file | Select-Object Name, Length, LastWriteTime, CreationTime, Attributes
# 获取详细属性(包括自定义属性)
$shell = New-Object -ComObject Shell.Application
$folder = $shell.Namespace("C:\path\to\folder")
$file = $folder.ParseName("yourfile.txt")
# 获取各种属性
$properties = @(
    "名称", "大小", "类型", "修改日期", "创建日期",
    "作者", "标题", "主题", "备注"
)
for ($i = 0; $i -lt $properties.Count; $i++) {
    $value = $folder.GetDetailsOf($file, $i)
    if ($value) {
        Write-Host "$($properties[$i]): $value"
    }
}

Python 脚本

import os
import stat
import datetime
def get_file_properties(filepath):
    # 获取基本属性
    stats = os.stat(filepath)
    properties = {
        '文件名': os.path.basename(filepath),
        '文件大小': f"{stats.st_size} bytes",
        '创建时间': datetime.fromtimestamp(stats.st_ctime),
        '修改时间': datetime.fromtimestamp(stats.st_mtime),
        '访问时间': datetime.fromtimestamp(stats.st_atime),
        '权限': oct(stat.S_IMODE(stats.st_mode)),
        '文件类型': '目录' if os.path.isdir(filepath) else '文件'
    }
    return properties
# 使用示例
file_path = r"C:\path\to\your\file.txt"
props = get_file_properties(file_path)
for key, value in props.items():
    print(f"{key}: {value}")

使用 Python + win32com 获取Windows扩展属性

import win32com.client
from datetime import datetime
def get_windows_file_properties(filepath):
    shell = win32com.client.Dispatch("Shell.Application")
    folder = shell.Namespace(os.path.dirname(filepath))
    file = folder.ParseName(os.path.basename(filepath))
    # Windows属性索引列表
    properties = [
        (0, "名称"), (1, "大小"), (2, "类型"), (3, "修改日期"),
        (4, "创建日期"), (6, "属性"), (9, "作者"), (10, "标题"),
        (11, "主题"), (12, "备注"), (14, "日期"), (20, "持续时间"),
        (21, "宽度"), (22, "高度"), (24, "分辨率"),
        (26, "相机型号"), (28, "焦距"), (34, "ISO")
    ]
    result = {}
    for prop_index, prop_name in properties:
        value = folder.GetDetailsOf(file, prop_index)
        if value:
            result[prop_name] = value
    return result

Linux/macOS Shell 脚本

#!/bin/bash
# 获取文件属性
file="$1"
echo "=== 文件属性 ==="
echo "文件名: $(basename "$file")"
echo "大小: $(stat -c%s "$file") bytes"
echo "文件类型: $(file -b "$file")"
echo "权限: $(stat -c%a "$file")"
echo "所有用户: $(stat -c%U "$file")"
echo "所属组: $(stat -c%G "$file")"
echo "最后访问: $(stat -c%x "$file")"
echo "最后修改: $(stat -c%y "$file")"
echo "状态变更: $(stat -c%z "$file")"
# 获取扩展属性(如果存在)
echo -e "\n=== 扩展属性 ==="
attr -l "$file" 2>/dev/null || echo "无扩展属性"

批量处理脚本(PowerShell)

# 批量获取文件夹内所有文件的属性并导出到CSV
$folderPath = "C:\your\folder"
$outputFile = "C:\file_properties.csv"
$results = Get-ChildItem -Path $folderPath -Recurse | ForEach-Object {
    [PSCustomObject]@{
        文件名 = $_.Name
        完整路径 = $_.FullName
        大小KB = [math]::Round($_.Length / 1KB, 2)
        创建时间 = $_.CreationTime
        修改时间 = $_.LastWriteTime
        访问时间 = $_.LastAccessTime
        属性 = $_.Attributes
        扩展名 = $_.Extension
    }
}
$results | Export-Csv -Path $outputFile -NoTypeInformation -Encoding UTF8
Write-Host "文件属性已导出到: $outputFile"

Node.js 脚本

const fs = require('fs');
const path = require('path');
function getFileProperties(filePath) {
    const stats = fs.statSync(filePath);
    return {
        fileName: path.basename(filePath),
        fileSize: `${stats.size} bytes`,
        isDirectory: stats.isDirectory(),
        isFile: stats.isFile(),
        createdTime: stats.birthtime,
        modifiedTime: stats.mtime,
        accessedTime: stats.atime,
        permissions: (stats.mode & parseInt('777', 8)).toString(8),
        fileExtension: path.extname(filePath)
    };
}
// 使用示例
const filePath = '/path/to/your/file.txt';
console.log(getFileProperties(filePath));

使用方法:

  1. Windows PowerShell: 将脚本保存为 .ps1 文件,右键点击选择"使用 PowerShell 运行"
  2. Python: 保存为 .py 文件,运行 python script.py
  3. Linux/macOS: 保存为 .sh 文件,运行 bash script.sh filename

选择适合你操作系统的脚本即可,如果需要获取特定类型的文件属性(如图片、文档等),建议使用 PowerShell 或 Python + win32com 方案。

抱歉,评论功能暂时关闭!