You need to enable JavaScript to run this app. 能否使用Deno修改文件元数据,包括修改时间与Windows创建时间?
优惠活动
大模型
产品
解决方案
定价
更多

能否使用Deno修改文件元数据,包括修改时间与Windows创建时间?

Deno文件元数据修改支持说明

目前Deno已经原生支持修改文件的访问时间(atime)和最后修改时间(mtime),无需调用系统shell工具实现:

  • 对应API为异步方法Deno.utime和同步方法Deno.utimeSync,入参依次为文件路径、目标访问时间、目标修改时间,时间支持传入Date对象或者秒级Unix时间戳,示例代码如下:
// 同步修改示例:将test.txt的访问时间、修改时间均设置为当前时间
Deno.utimeSync("./test.txt", Date.now() / 1000, Date.now() / 1000);

// 异步修改示例:指定为2024年1月1日零点
const targetTime = new Date("2024-01-01T00:00:00");
await Deno.utime("./test.txt", targetTime, targetTime);
  • 使用上述API需要给Deno运行时添加--allow-write权限。

如果你需要修改Windows系统下特有的文件创建时间,目前Deno稳定版暂未原生支持该操作,需要调用系统原生工具实现,示例如下:

// 需添加--allow-run运行权限
const filePath = "C:\\test\\demo.txt";
const targetCreateTime = "2024-01-01 00:00:00";
const cmd = new Deno.Command("powershell", {
  args: [
    "-Command",
    `(Get-Item '${filePath}').CreationTime = '${targetCreateTime}'`
  ]
});
await cmd.output();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 08:45:03