[BAT]在windows下快速刪除過期檔案

vbs, jscript, bat等 script 語言-非 web 使用的討論區
回覆文章
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

[BAT]在windows下快速刪除過期檔案

文章 tim »

在 linux 下有方便的 shell 指令可用, 在 windows 一下, 一樣有好用的指令 forfiles 可供應用, 若是要將某目錄下的檔案, 超過幾天以上的時間就要進行刪除(像是供份檔, log 檔案等), 可以利用以下指令:

代碼: 選擇全部

forfiles /p c:\logs /D -7 /C "cmd /c del /f @file"
上面的指令是利用 forfiles 將 c:\logs 目錄下, /D -7 指7天(含)前的檔案列舉出來, 而利用 /C "cmd /c del /f @file" 可以將列舉出來的檔案利用指令刪除. 若不放心指令的狀況, 可以使用 echo 指令列出檔案名, 如下:

代碼: 選擇全部

forfiles /p c:\logs /D -7 /C "cmd /c echo @file"
利用 forfiles 可以方便地將想找尋某些條件的檔案列舉並進行刪除/搬移/複製等動作, 很方便在 batch 檔應用的指令.

以下為 forfiles 的指令用法, 是以 forfiles /? 查出:

代碼: 選擇全部

FORFILES [/P pathname] [/M searchmask] [/S]
         [/C command] [/D [+ | -] {MM/dd/yyyy | 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
                        "MM/dd/yyyy" 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 01/01/2001
             /C "cmd /c echo @fname is new since Jan 1st 2001"
    FORFILES /D +4/1/2012 /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"

多多留言, 整理文章, 把經驗累積下來.....
頭像
tim
文章: 1380
註冊時間: 2008年 11月 26日, 00:49

Re: [BAT]在windows下快速刪除過期檔案

文章 tim »

若是需要刪除過期檔案, 但又想保留每日備份中的每月1號的備份檔, 也可以利用這個 FORFILES 指令來操作, 不過必須配合 batch 檔案來進行, 如下:

FORFILES 指令:

代碼: 選擇全部

FORFILES /P C:\BACKUP /D -30 /C "CMD /C RUN.BAT @fdate @file"
上面的指令是30日以上, 列出所以的檔案日期及檔名給 RUN.BAT 批次檔

在 RUN.BAT 中指令如下:

代碼: 選擇全部

@ECHO OFF
SET MYDATE=%1
SET MYFILE=%2
IF NOT %MYDATE:~-2%==/1 DEL %MYFILE% 
上面的 RUN.BAT 利用兩個變數收到前面的參數後, 再利用取出 MYDATE 的最後2個字元的方式來判斷是否為每月1號的檔案, 並配合 NOT 來進行檔案刪除的動作, 保留下每月1號的檔案.

由於 FORFILES 後的指令必須為一字串, 原本打算用 /V 的方式來進行變數比對, 但因為不能再 CMD /C "xxxxxx" 這樣的方式進行指令撰寫, 所以改用呼叫 BAT 檔案, 傳遞參數的方式進行.
多多留言, 整理文章, 把經驗累積下來.....
回覆文章