批处理文件删除超过N天的文件 [英] Batch file to delete files older than N days

查看:51
本文介绍了批处理文件删除超过N天的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来删除批处理文件中超过 7 天的所有文件.我在网上搜索过,找到了一些包含数百行代码的示例,还有一些示例需要安装额外的命令行实用程序才能完成任务.

I am looking for a way to delete all files older than 7 days in a batch file. I've searched around the web, and found some examples with hundreds of lines of code, and others that required installing extra command line utilities to accomplish the task.

类似的事情可以在 BASH 中完成 只需几行代码.似乎可以对 Windows 中的批处理文件进行至少远程简单的操作.我正在寻找一种在标准 Windows 命令提示符下工作的解决方案,无需任何额外的实用程序.请不要使用 PowerShell 或 Cygwin.

Similar things can be done in BASH in just a couple lines of code. It seems that something at least remotely easy could be done for batch files in Windows. I'm looking for a solution that works in a standard Windows command prompt, without any extra utilities. Please no PowerShell or Cygwin either.

推荐答案

享受:

forfiles -p "C:whatever" -s -m *.* -d <number of days> -c "cmd /c del @path"

有关详细信息,请参阅forfiles 文档.

See forfiles documentation for more details.

更多好东西,请参考Windows XP 命令行的 AZ 索引.

For more goodies, refer to An A-Z Index of the Windows XP command line.

如果您的机器上没有安装 forfiles,请从任何 Windows Server 2003 到您的 Windows XP 机器 %WinDir%system32.这是可能的,因为 EXE 在 Windows Server 2003 和 Windows XP 之间完全兼容.

If you don't have forfiles installed on your machine, copy it from any Windows Server 2003 to your Windows XP machine at %WinDir%system32. This is possible since the EXE is fully compatible between Windows Server 2003 and Windows XP.

更高版本的 Windows 和 Windows Server 默认安装它.

Later versions of Windows and Windows Server have it installed by default.

对于 Windows 7 和更新版本(包括 Windows 10):

For Windows 7 and newer (including Windows 10):

语法略有变化.因此更新的命令是:

The syntax has changed a little. Therefore the updated command is:

forfiles /p "C:whatever" /s /m *.* /D -<number of days> /C "cmd /c del @path"

这篇关于批处理文件删除超过N天的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆