使用forfiles命令批量刪除N天前文件
在整理手上幾臺(tái)SQL SERVER 2000的數(shù)據(jù)庫(kù)備份時(shí),一方面為了方便快速還原數(shù)據(jù)庫(kù),另外一方面為了備份冗余、備份方式統(tǒng)一(先備份到本地,然后收上磁帶),將以前通過(guò)Symantec Backup Exec直接備份上帶的作業(yè)改成了如下方式:
Step 1: 通過(guò)數(shù)據(jù)庫(kù)維護(hù)計(jì)劃將備份生成在本地磁盤(pán)M,完整備份保留2天,事務(wù)日志備份保留3天
M:\DB_BACKUP\FULL_BACKUP
M:\DB_BACKUP\LOG_BACKUP
Step 2: 備份完成后通過(guò)Symantec Backup Exec將備份文件收上磁帶。
但是發(fā)現(xiàn)即使SQL SERVER 2000的數(shù)據(jù)庫(kù)維護(hù)計(jì)劃設(shè)置了刪除幾天前的備份文件,但是發(fā)現(xiàn)根本沒(méi)有刪除過(guò)期備份。于是只好使用dos命令來(lái)處理。剛開(kāi)始想用forfiles命令,結(jié)果我搜索的時(shí)候,發(fā)現(xiàn)Windows 2000下沒(méi)有forfiles命令,后來(lái)通過(guò)從第三方復(fù)制過(guò)來(lái),發(fā)現(xiàn)Windows 2000下也可以使用forfiles(如果不借助于forfiles命令,直接用批處理命令完成這個(gè),那簡(jiǎn)直痛苦死了)
關(guān)于forfiles命令的語(yǔ)法如下所示
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"
對(duì)應(yīng)的中文提示信息如下所示:
語(yǔ)法
forfiles [/p Path ] [/m SearchMask ] [/s ] [/c Command ] [/d [{+ | - }] [{MM / DD / YYYY | DD }]]
參數(shù)
/p Path
指定Path ,表明要從哪里開(kāi)始搜索。默認(rèn)的文件夾是當(dāng)前工作目錄,該目錄通過(guò)鍵入句號(hào)(.) 指定。
/m SearchMask
按照SearchMask 搜索文件。默認(rèn)的SearchMask 是*.* 。
/s
指示forfiles 在子目錄中搜索。
/c Command
在每個(gè)文件上運(yùn)行指定的Command 。帶有空格的命令字符串必須用引號(hào)括起來(lái)。默認(rèn)的Command 是"cmd /c echo @file" 。
/d [{+ | - }] [{MM / DD / YYYY | DD }]
選擇日期大于或等于(+ )(或者小于或等于(- ))指定日期的文件,其中MM / DD / YYYY 是指定的日期,DD 是當(dāng)前日期減去DD 天。如果未指定+ 或- ,則使用+ 。DD 的有效范圍是0 - 32768。
/?
在命令提示符下顯示幫助。
如下所示,由于Windows Server 2000下拷貝過(guò)來(lái)的forfiles命令的版本是V 1.1,使用參數(shù)必須為-p、-c、-m 而且參數(shù)后面不能有空格。
如下所示,delete_old_backup.bat 刪除2天前的完整備份、事務(wù)日志備份、以及維護(hù)計(jì)劃生成的日志文件。
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
腳本編寫(xiě)、測(cè)試成功后,然后設(shè)置Task Schedule,大體步驟如下步驟所示:
Step 1:在控制面板找到任務(wù)計(jì)劃,執(zhí)行任務(wù)計(jì)劃向?qū)В?/p>
Step 2: 點(diǎn)擊“瀏覽”按鈕,選擇M:\DB_BACKUP\delete_old_backup.bat文件
Step 3:輸入任務(wù)的名稱(chēng),以及執(zhí)行任務(wù)的Schedule
Step 4:設(shè)置任務(wù)執(zhí)行的時(shí)間以及頻率
Step 5:輸入知曉計(jì)劃任務(wù)的賬號(hào)以及密碼
Step 6:完成任務(wù)計(jì)劃設(shè)置。
上面的bat主要是支持參數(shù)與日志的現(xiàn)實(shí),確實(shí)不錯(cuò),其實(shí)核心代碼就是這樣的
forfiles /p "c:\backup" /d -10 /c "cmd /c echo deleting @file ... && del /f @path"
測(cè)試前最好備份一下數(shù)據(jù)
循環(huán)刪除3天前的文件
Forfiles /S /P e:\log\weblog\ /M *.* /D -3 /C "cmd /c del /S /Q @file"
通過(guò)批處理文件刪除超過(guò)3天的文件和文件夾
批處理文件是一種用于自動(dòng)化執(zhí)行一系列命令或操作的腳本文件。它可以在Windows操作系統(tǒng)中使用,通過(guò)命令行界面執(zhí)行。
刪除超過(guò)3天的文件和文件夾可以通過(guò)批處理文件來(lái)實(shí)現(xiàn)。下面是一個(gè)示例的批處理文件內(nèi)容:
@echo off setlocal rem 設(shè)置要?jiǎng)h除的目錄路徑 set "target_directory=C:\path\to\directory" rem 設(shè)置要?jiǎng)h除的文件和文件夾的最大存活時(shí)間(單位:天) set max_age=3 rem 獲取當(dāng)前日期和時(shí)間 for /f "tokens=1-3 delims=/ " %%a in ('date /t') do ( set "current_date=%%c-%%a-%%b" ) for /f "tokens=1-2 delims=: " %%a in ('time /t') do ( set "current_time=%%a%%b" ) rem 遍歷目標(biāo)目錄下的所有文件和文件夾 for /d %%i in ("%target_directory%\*") do ( rem 獲取文件夾的創(chuàng)建日期 for %%j in ("%%i\..") do set "folder_date=%%~tj" set "folder_date=!folder_date:~0,10!" rem 計(jì)算文件夾的存活時(shí)間 call :date_diff "!folder_date!" "!current_date!" days folder_age rem 如果文件夾的存活時(shí)間超過(guò)最大存活時(shí)間,則刪除文件夾及其內(nèi)容 if !folder_age! gtr %max_age% ( echo Deleting folder: %%i rd /s /q "%%i" ) ) rem 遍歷目標(biāo)目錄下的所有文件 for %%i in ("%target_directory%\*") do ( rem 獲取文件的創(chuàng)建日期 for %%j in ("%%i") do set "file_date=%%~tj" set "file_date=!file_date:~0,10!" rem 計(jì)算文件的存活時(shí)間 call :date_diff "!file_date!" "!current_date!" days file_age rem 如果文件的存活時(shí)間超過(guò)最大存活時(shí)間,則刪除文件 if !file_age! gtr %max_age% ( echo Deleting file: %%i del "%%i" ) ) endlocal exit /b :date_diff rem 計(jì)算日期差異的函數(shù) setlocal set "date1=%~1" set "date2=%~2" set "unit=%~3" set "diff=" rem 將日期轉(zhuǎn)換為秒數(shù) for /f "tokens=1-3 delims=-" %%a in ("%date1%") do ( set /a "date1_sec=(((%%c*12)+1)*100+%%a)*100+%%b" ) for /f "tokens=1-3 delims=-" %%a in ("%date2%") do ( set /a "date2_sec=(((%%c*12)+1)*100+%%a)*100+%%b" ) rem 計(jì)算日期差異 set /a "diff_sec=date1_sec-date2_sec" if "%unit%"=="days" ( set /a "diff=diff_sec/86400" ) endlocal & set "%4=%diff%" & exit /b
到此這篇關(guān)于使用forfiles命令批量刪除N天前文件的文章就介紹到這了,更多相關(guān)forfiles 批量刪除N天前文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
批處理bat設(shè)置固定IP地址和自動(dòng)IP地址
這篇文章主要介紹了通過(guò)批處理bat設(shè)置固定IP地址和自動(dòng)IP地址的方法,需要的朋友可以參考下2014-07-07批處理bat遞歸計(jì)算N!的實(shí)現(xiàn)代碼
這篇文章主要介紹了批處理bat遞歸計(jì)算N!的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-12-12用批處理實(shí)現(xiàn)禁止運(yùn)行指定程序
原理就是通過(guò)修改注冊(cè)表來(lái)實(shí)現(xiàn)的,HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options2008-08-08學(xué)會(huì)批處理,用心學(xué)很容易!另一不錯(cuò)的見(jiàn)解
學(xué)會(huì)批處理,用心學(xué)很容易!另一不錯(cuò)的見(jiàn)解...2007-03-03ARP實(shí)現(xiàn)本機(jī)綁定的批處理文件
ARP實(shí)現(xiàn)本機(jī)綁定的批處理文件...2007-08-08