在整理手上几台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

脚本编写、测试成功后,然后设置Task Schedule,大体步骤如下步骤所示:

Step 1:在控制面板找到任务计划,执行任务计划向导:

Step 2: 点击“浏览”按钮,选择M:\DB_BACKUP\delete_old_backup.bat文件

Step 3:输入任务的名称,以及执行任务的Schedule

Step 4:设置任务执行的时间以及频率

Step 5:输入知晓计划任务的账号以及密码

Step 6:完成任务计划设置。

forfiles命令批量删除N天前文件的更多相关文章

  1. ElasticSearch 定时批量删除N天前的数据

    描述: 之前我已经完成了使用ElasticSearch.kibana.filebeat.三个工具完成分布式集群收集 分布在各个ip地址上的微服务日志,这样就可以统一的在一个服务器上查看了所有的微服务产 ...

  2. 通过bat命令批量删除VS查找历史记录

    有时候我们会发现我们的VS查找下拉框里面有很多之前的搜索记录,想删除但是却没有地方删除.  网上的方法都是直接找到注册表HKEY_CURRENT_USER\Software\Microsoft\Vis ...

  3. 用bat批处理程序通过DOS命令行删除所有的空文件夹

    用过gothub或者码云的同学都知道,不包含任何文件的空文件夹上传提交时不被允许的.当然你可以在空文件下创建.keep文件(或.gitkeep文件),然后就可以上传了. 但是如果空文件夹比较多,并且我 ...

  4. linux命令行删除N天前的数据的命令

    命令:  find . -mtime +N -type f -name "*.log.*" -exec rm -f {} \; 简单解释: find .查询  ; -mtime 规 ...

  5. SQLServer 命令批量删除数据库中指定表(游标循环删除)

    DECLARE @tablename VARCHAR(30),@sql VARCHAR(500)DECLARE cur_delete_table CURSOR READ_ONLY FORWARD_ON ...

  6. shell命令批量创建指定格式的文件

    shell命令批量创建文件 [root@w212 test]# for count in `seq 9` ;do echo "$count" > a.2018050$coun ...

  7. linux 自动删除n天前文件

    现在系统每天生成一个日期文件夹,并压缩上传到ftp服务器,造成目录下文件太多,所以决定写个定时删除文件的任务 写脚本文件 find /home/data -mtime +90 -name " ...

  8. forfiles命令批处理删除过期文件

    命令格式: forfiles.exe /p "D:\备份" /s /m *.zip /d -7 /c "cmd /c del @path" /p:指定目录 /s ...

  9. Linux 按时间批量删除文件(删除N天前文件)

    需要根据时间删除这个目录下的文件,/home/lifeccp/dicom/studies,清理掉20天之前的无效数据. 可以使用下面一条命令去完成: -name "*.*" -ex ...

随机推荐

  1. [Latex]实现行内高亮

    Latex的行内高亮 前两天想要在做的小操作系统实验指导书里使用行内高亮,一开始虽然有命令 \mint{Language}|contents| 但是无奈只能实现跳行高亮,即不能实现行内高亮.即代码高亮 ...

  2. DP入门---Robberies

    HDU  2955 Description The aspiring Roy the Robber has seen a lot of American movies, and knows that ...

  3. Java中检测字符串的编码格式

    public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equ ...

  4. git本地提交项目到你的github

    第一步:建立git仓库(若已经创建则跳过该步) cd到你的本地项目根目录下,执行git命令 git init 第二步:将项目的文件添加到仓库中 git add fileName 如果想添加本次仓库中的 ...

  5. 硅谷新闻3--使用Android系统自带的API解析json数据

    NewsCenterPagerBean2 bean2 = new NewsCenterPagerBean2(); try { JSONObject object = new JSONObject(js ...

  6. 【GOF23设计模式】代理模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_代理模式.静态代理 package com.test.proxy.staticProxy; public interfac ...

  7. angularJS中的$apply(),$digest(),$watch()

    $apply()和$digest()在AngularJS中是两个核心概念,但是有时候它们又让人困惑.而为了了解AngularJS的工作方式,首先需要了解$apply()和$digest()是如何工作的 ...

  8. javascript基础系列(入门前须知)

    -----------------------小历史---------------------------- javascript与java是两种语言,他们的创作公司不同,JavaScript当时是借 ...

  9. javascript --- 词法分析

    JavaScript代码自上而下执行,但是在js代码执行前,会首先进行词法分析,所以事实上,js运行要分为词法分析和执行两个阶段. 词法分析主要分为三步: 第一步: 分析形参: 第二步: 分析变量声明 ...

  10. 详解HTML5中rel属性的prefetch预加载功能使用

    在HTML5中,有个很有用但常被忽略的特性,就是预先加载(prefetch),它的原理是: 利用浏览器的空闲时间去先下载用户指定需要的内容,然后缓存起来,这样用户下次加载时,就直接从缓存中取出来,效率 ...