天天看點

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

                        @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

                        @ftime   - returns the last modified time of the

                        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 而且參數後面不能有空格。

forfiles指令批量删除N天前檔案

如下所示,delete_old_backup.bat 删除2天前的完整備份、事務日志備份、以及維護計劃生成的日志檔案。

腳本編寫、測試成功後,然後設定task schedule,大體步驟如下步驟所示:

step 1:在控制台找到任務計劃,執行任務計劃向導:

forfiles指令批量删除N天前檔案

step 2: 點選“浏覽”按鈕,選擇m:\db_backup\delete_old_backup.bat檔案

forfiles指令批量删除N天前檔案

step 3:輸入任務的名稱,以及執行任務的schedule

forfiles指令批量删除N天前檔案

step 4:設定任務執行的時間以及頻率

forfiles指令批量删除N天前檔案

step 5:輸入知曉計劃任務的賬号以及密碼

forfiles指令批量删除N天前檔案

step 6:完成任務計劃設定。

forfiles指令批量删除N天前檔案