Core code:
@echo off: by oicu#lsxk.org:: 15:17 2011-1-13:: If only delete the specified date or n days before the modified file, Forfiles can forfiles/p "c:\ Test directory"/s/m *.log/d -3/C "cmd/c if @isdir ==false echo del @file:: File modified before date specified (included):/D-YYYY/MM/DD:: The following are chronological:: Usage Restrictions: Control Panel set date format in Chinese (China)::
Only DIR/TC can see the file creation time, the default dir is DIR/TW:: In order to remove the creation time, had to use two times for+find, must be very slow. CD/D your directory:: If you want to include subdirectories, use FOR/R. %%a in (*):: Displays the file modification time for the%%a in (*) do echo "%%~ta":: Chinese format equivalent to for%%a in (*) doing for/f "tokens=1,2* delims="%%b in (' Dir/tw "%%a" ^| find/i "%%~nxa") do echo "%%b%%c":: Delete a modified file after a certain time (this simple) for%%a in (*) do if "%%~ta" GTR "2008-0 4-01 "Echo del"%%a:: This is the display file creation time for%%a in (*) does for/f "tokens=1,2* delims="%%b in (' Dir/tc "%%a find/i "%%~nxa" "Do Echo"%%b%%c:: Delete a file created after a certain time, if you want to delete a time created before, change two:: Place GTR for LSS.
In addition to the date, increase the judgment time, you can remove the Else section. For%%a in (*) doing for/f "tokens=1,2* delims="%%b in (' DIR/TC '%%a "^| find/i "%%~nxa" "Do" (if "%%b" GTR "2010-12-15" (Echo del "%%a") Else (if "%%b" Equ "2010-12-15 "If"%%c "gtr" 14:50 "Echo del"%%a "):: The batch calculation time is limited by the date format of the system, affecting the value of a field with a set:: The batch calculation time method is too complex to either delete the file with the VBS script or the batch
:: Delete with VBS script. :: Take 10 days before the date, retrieve put the variable olddate echo WScript.Echo DateAdd ("D", -10,date) >getolddate.vbs for/f,%%a in (' Cscript/nologo Geto Lddate.vbs ') do set olddate=%%a echo%olddate% del getolddate.vbs:: VBScript does not have the format function, year, Month, day and other functions to take a value after the small:: 1 0 number, you can only use the following method to organize the date format echo WScript.Echo DateAdd ("D", -10,date) >getolddate.vbs for/f "tokens=1,2,3* delims=-/."%%i in ( ' Cscript/nologo getolddate.vbs ') do (set y=%%i set m=%%j set d=%%k) if%m% LSS ten set m=0%m% if%d% LSS set d= 0%d% set olddate=%y%-%m%-%d% echo%olddate% del getolddate.vbs:: Synthesis above, finally get the result we want:: delete old files created 10 days ago (excluding subdirectories) echo WSCRIPT.E Cho DateAdd ("D", -10,date) >getolddate.vbs for/f "tokens=1,2,3* delims=-/."%%i in (' Cscript/nologo getolddate.vbs ') d O (set y=%%i set m=%%j set d=%%k) if%m% LSS ten set m=0%m% if%d% LSS ten set d=0%d% set olddate=%y%-%m%-%d% del Ge tolddate.vBS for%%a in (*) doing for/f "tokens=1,2* delims="%%b in (' Dir/tc "%%a find/i "%%~nxa" "Do" (if "%%b" LSS "%olddate%" Echo del "%%a"):: Delete old files modified 10 days ago (excluding subdirectories) echo WScript.Echo DateAdd ("D",
-10,date) >getolddate.vbs for/f "tokens=1,2,3* delims=-/."%%i in (' Cscript/nologo getolddate.vbs ') do (set y=%%i Set M=%%J set D=%%k) if%m% LSS ten set m=0%m% if%d% LSS ten set d=0%d% set olddate=%y%-%m%-%d% del getolddate.vbs for
%%a in (*) do if "%%~ta" LSS "%olddate%" Echo del "%%a": This article all use the Echo del instead of Del, just show the file to be deleted, not actually deleted. Pause goto:eof