forfiles命令批量删除N天前文件
在整理手上几台SQL SERVER 2000的数据库备份时,一方面为了方便快速还原数据库,另外一方面为了备份冗余、备份方式统一(先备份到本地,然后收上磁带),将以前通过Symantec Backup Exec直接备份上带的作业改成了如下方式:
Step 1: 通过数据库维护计划将备份生成在本地磁盘M,完整备份保留2天,事务日志备份保留3天
M:\DB_BACKUP\FULL_BACKUP
M:\DB_BACKUP\LOG_BACKUP
Step 2: 备份完成后通过Symantec Backup Exec将备份文件收上磁带。
是发现即使SQL SERVER 2000的数据库维护计划设置了删除几天前的备份文件,但是发现根本没有删除过期备份。于是只好使用dos命令来处理。刚开始想用forfiles命令, 结果我搜索的时候,发现Windows 2000下没有forfiles命令,后来通过从第三方复制过来,发现Windows 2000下也可以使用forfiles(如果不借助于forfiles命令,直接用批处理命令完成这个,那简直痛苦死了)关于forfiles命令的语法如下所示
C:\ forfiles /?
FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {yyyy-MM-dd | dd}]
Description:
Selects a file (or set of files) and executes a
command on that file. This is helpful for batch jobs.
Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
directory (.).
/M searchmask Searches files according to a searchmask.
The default searchmask is * .
/S Instructs forfiles to recurse into
subdirectories. Like "DIR /S".
/C command Indicates the command to execute for each file.
Command strings should be wrapped in double
quotes.
The default command is "cmd /c echo @file".
The following variables can be used in the
command string:
@file - returns the name of the file.
@fname - returns the file name without
extension.
@ext - returns only the extension of the
file.
@path - returns the full path of the file.
@relpath - returns the relative path of the
file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in
bytes.
@fdate - returns the last modified date of the
file.
@ftime - returns the last modified time of the
file.
To include special characters in the command
line, use the hexadecimal code for the character
in 0xHH format (ex. 0x09 for tab). Internal
CMD.exe commands should be preceded with
"cmd /c".
/D date Selects files with a last modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"yyyy-MM-dd" format; or selects files with a
last modified date greater than or equal to (+)
the current date plus "dd" days, or less than or
equal to (-) the current date minus "dd" days. A
valid "dd" number of days can be any number in
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 而且参数后面不能有空格。
如下所示,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_BACKUP\FULL_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_BACKUP\LOG_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
潇湘隐者 网名潇湘隐者/潇湘剑客、英文名Kerry,兴趣广泛,广泛涉猎,个性随意,不善言辞。执意做一名会写代码的DBA,混迹于IT行业
相关文章
- python之文件处理
- TS文件解码TS文件解密TS流批量下载和解码工具
- linux批量文件重命名命令_shell重命名文件指令
- 用python的shutil库批量移动文件及批量重命名
- bat批量删除文件后缀_怎么批量删除文件名中的数字
- Python批量修改文件名,文件再多也只要一秒,省时又不闹心
- Python批量爬取微信公众号文章中的图片重建PowerPoint文件
- 使用Python批量筛选上千个Excel文件中的某一行数据并另存为新Excel文件(上篇)
- C#实现xlsx文件导出
- 文件MySQL删除文件的强制策略(mysql强制删除)
- Linux给文件分配权限的技术实践(linux给文件写权限)
- 如何取得用户参数文件中的parameters-[BAPI_USER_GET_DETAIL]详解编程语言
- 利用Linux快速完成批量文件删除(linux批量删除命令)
- 文件Linux下批量处理文件的简单技巧(linux批量处理)
- 复制批量复制文件:Linux的cp命令简介(linuxcp批量)
- Linux 下批量文件重命名指南(批量重命名linux)
- 轻松实现Linux批量文件重命名(批量重命名linux)
- Linux下的批量文件重命名(批量重命名linux)
- 自动安装的应答文件
- xCHM 是跨平台的 CHM 文件查看器,是免费开源软件。主要特性包括:显示目录、打印页面、改变字体及大小、支持书签和搜索等等。
- Linux文件传输指南:快速上传和下载文件的方法(如何把文件传到linux)
- Linux文件系统之NFS(linux文件nfs)
- asp.net实现文件下载的代码
- php中批量修改文件后缀名的函数代码
- php实现批量下载百度云盘文件例子分享
- PHP批量删除、清除UTF-8文件BOM头的代码实例
- 利用php下载xls文件(自己动手写的)
- nodejs批量修改文件编码格式