How to use batch processing in dos to delete files created and modified N days ago or within a specified date and time
This article introduces how to use batch processing in dos to delete files created and modified N days ago or within a specified date and time, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Core code:
@ echo off:: by oicu#lsxk.org:: 15:17 2011-1-13: if you only delete files modified on a specified date or N days ago Use forfiles to FORFILES / P "C:\ test directory" / S / M * .log / D-3 / C "cmd / c if @ isdir==FALSE echo del @ file":: files modified before the specified date (inclusive): / D-yyyy/mm/dd:: the following is the current account:: use restrictions: set the date format to Chinese in the control panel (China):: only dir / tc can view the file creation time The default dir is dir / tw::. In order to retrieve the creation time, you have to use for+find twice, which must be very slow. Cd / d your directory: if you want to include subdirectories, use for / r. % an in (*):: display file modification time for% an in (*) do echo "% ~ ta": equivalent to for% an in (*) do for / f "tokens=1,2* delims="% b in ('dir / tw "% a" ^ | find / I "% ~ nxa"') do echo "% b% c":: delete files modified after a certain time (this simple) for % an in (*) do if "% ~ ta" gtr "2008-04-01" echo del "% a":: this is the file creation time for% an in (*) do for / f "tokens=1,2* delims="% b in ('dir / tc "% a" ^ | find / I "% ~ nxa"') do echo "% b% c":: delete files created after a certain time If you want to delete something created before a certain time, change the gtr to lss at two::. In addition to the date, by increasing the judgment time, the else segment can be removed. For% an in (*) do 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"): batch The calculation time is limited by the date format of the system Affect the use of set to get the value of a field,:: batch calculation time method is too complex, either use only vbs script to delete files, or batch:: with vbs script to delete. :: take the date 10 days ago, retrieve the variable OldDateecho wscript.echo dateadd ("d",-10 date) > GetOldDate.vbsfor / f% an in ('cscript / nologo GetOldDate.vbs') do set OldDate=%%aecho% OldDate%del GetOldDate._vbs:: VBScript has no Format function, and Year, Month, Day and other functions have to deal with the number that is smaller than:: 10 after taking the value. You can only organize the date format echo wscript.echo dateadd ("d",-10 tokens=1,2,3* delims=-/. date) > GetOldDate.vbsfor / f "date in the following way "%% I in ('cscript / nologo GetOldDate.vbs') do (set yearly%% I set masking%% j set dudes% k) if% m% LSS 10 set m=0%m%if% d% LSS 10 set d=0%d%set OldDate=%y%-%m%-%d%echo% OldDate%del GetOldDate._vbs:: synthesis above Finally, we get the desired result: delete the old file (excluding subdirectories) created 10 days ago echo wscript.echo dateadd ("d",-10 tokens=1,2,3* delims=-/. date) > GetOldDate.vbsfor / f "date "%% I in ('cscript / nologo GetOldDate.vbs') do (set yawning%% I set masking%% j set dudes% k) if% m% LSS 10 set m=0%m%if% d% LSS 10 set d=0%d%set OldDate=%y%-%m%-%d%del GetOldDate.vbsfor%% an in (*) do 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 (excluding subdirectories) modified 10 days ago echo wscript.echo dateadd ("d") -10 tokens=1,2,3* delims=-/. date) > GetOldDate.vbsfor / f "date "%% I in ('cscript / nologo GetOldDate.vbs') do (set yawning%% I set set% j set dwells% k) if% m% LSS 10 set m=0%m%if% d% LSS 10 set d=0%d%set OldDate=%y%-%m%-%d%del GetOldDate.vbsfor% an in (*) do if"%% ~ ta "lss"% OldDate% "echo del"% a ":: this article uses echo del instead of del, only showing the files to be deleted It is not actually deleted. Pausegoto: eof on how to use batch processing to delete files created and modified N days ago or within a specified date and time in dos. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.