使用forfiles命令批量删除N天前文件

2019-01-29 22:01:49于海丽

                        the range of 0 - 32768.
                        "+" is taken as default sign if not specified.
    /?                  Displays this help message.

Examples:

    FORFILES /?
    FORFILES
    FORFILES /P C:WINDOWS /S /M DNS*.*
    FORFILES /S /M *.txt /C "cmd /c type @file | more"
    FORFILES /P C: /S /M *.bat
    FORFILES /D -30 /M *.exe
             /C "cmd /c echo @path 0x09 was changed 30 days ago"
    FORFILES /D 2001-01-01
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +2014-12-15 /C "cmd /c echo @fname is new today"
    FORFILES /M *.exe /D +1
    FORFILES /S /M *.doc /C "cmd /c echo @fsize"
    FORFILES /M *.txt /C "cmd /c if @isdir==FALSE notepad.exe @file"
 
对应的中文提示信息如下所示:

语法
forfiles [/p Path ] [/m SearchMask ] [/s ] [/c Command ] [/d [{+ | - }] [{MM / DD / YYYY | DD }]]
参数
/p Path
指定Path ,表明要从哪里开始搜索。默认的文件夹是当前工作目录,该目录通过键入句号(.) 指定。
/m SearchMask
按照SearchMask 搜索文件。默认的SearchMask 是*.* 。
/s
指示forfiles 在子目录中搜索。
/c Command
在每个文件上运行指定的Command 。带有空格的命令字符串必须用引号括起来。默认的Command 是"cmd /c echo @file" 。
/d [{+ | - }] [{MM / DD / YYYY | DD }]
选择日期大于或等于(+ )(或者小于或等于(- ))指定日期的文件,其中MM / DD / YYYY 是指定的日期,DD 是当前日期减去DD 天。如果未指定+ 或- ,则使用+ 。DD 的有效范围是0 - 32768。
/?
在命令提示符下显示帮助。
如下所示,由于Windows Server 2000下拷贝过来的forfiles命令的版本是V 1.1,使用参数必须为-p、-c、-m 而且参数后面不能有空格。

clipboard 

如下所示,delete_old_backup.bat 删除2天前的完整备份、事务日志备份、以及维护计划生成的日志文件。

相关文章 大家在看
echo --------------------------------------------- >>delete_old_backup.log echo Delete the backup log start at %Date% - %time% >>delete_old_backup.log rem Delete days. set DaysAgo=2 rem delete old backup log files. set LogPath=M:DB_BACKUP forfiles -p%LogPath% -m*.txt -d-%DaysAgo% -c"cmd /c del /q @FILE" >> delete_old_backup.log echo Delete the backup log Stop at %Date% - %time% >>delete_old_backup.log echo Delete the full backup start at %Date% - %time% >>delete_old_backup.log set FullBackupPath=M:DB_BACKUPFULL_BACKUP forfiles -p%FullBackupPath% -m*.bak -d-%DaysAgo% -c"cmd /c del /q @FILE" >> delete_old_backup.log echo Delete the full backup Stop at %Date% - %time% >>delete_old_backup.log echo Delete the log backup start at %Date% - %time% >>delete_old_backup.log set LogBackupPath=M:DB_BACKUPLOG_BACKUP forfiles -p%LogBackupPath% -m*.TRN -d-%DaysAgo% -c"cmd /c del /q @FILE" >> delete_old_backup.log echo Delete the log backup Stop at %Date% - %time% >>delete_old_backup.log echo --------------------------------------------- >>delete_old_backup.log