First I'll show you a batch way to ALMOST do it.. But it isn't easy or flexible. I'll show deletions based on month. I'll suppose you want to keep SOME old records. But if you delete anything older than the current month, you may not have much left on the first day of the month! More reasonable is to keep the current month's files AND the previous month's. First you have to get the current date. I suggest something like this:
@echo off echo.|date|find "Current" > date.txt For %%x in (01 02 03 04 05 06 07 08 09 10 11 12) do call finddate.bat %%x del date.txtNow you have the current date in the file "date.txt". We'll use it later. For now we call a batch file "finddate.bat" twelve times and pass it a different number each time. We'll find one of those numbers in the date.txt file as the month. Since the month is always the first number in the numeric date, it will always be preceeded by a "space" and followed by a "dash" in the output of the DATE command (And in our date.txt file!). That makes it easy to spot. Here's the "finddate.bat":
:: Two-digit number (month) is passed in as %1 :: date.txt file contains the output of the DATE command :: Look for the tentative month inside the date.txt file. :: If not correct month, bail out. type date.txt | find " %1-" if errorlevel 1 goto DONE :: Turn off long filename expansion for the FOR command. :: Long file names cannot be passed as command-line arguments. lfnfor off :: Check all files. For each file, call delete.bat, :: passing it a filename and the current 2-digit month for %%y in (*.*) do call killold.bat %%y %1 :DONENow you need a "killold.bat" that will do the actual deletions:
:: Filename is passed in as %1, current month is %2 :: Don't delete if file date is current month. We can :: check file month by looking for the date info :: in the output of the DIR command for the file. dir %1 | find " %2-" if not errorlevel 1 goto DONE :: Now check to see if file is from previous :: month. If it isn't, delete it. if "%2"=="01" goto JANUARY if "%2"=="02" goto FEBRUARY if "%2"=="03" goto MARCH if "%2"=="04" goto APRIL if "%2"=="05" goto MAY if "%2"=="06" goto JUNE if "%2"=="07" goto JULY if "%2"=="08" goto AUGUST if "%2"=="09" goto SEPTEMBER if "%2"=="10" goto OCTOBER if "%2"=="11" goto NOVEMBER if "%2"=="12" goto DECEMBER :JANUARY dir %1 | find " 12-" if not errorlevel 1 goto DONE goto DELETE :FEBRUARY dir %1 | find " 01-" if not errorlevel 1 goto DONE goto DELETE :MARCH dir %1 | find " 02-" if not errorlevel 1 goto DONE goto DELETE :APRIL dir %1 | find " 03-" if not errorlevel 1 goto DONE goto DELETE :MAY dir %1 | find " 04-" if not errorlevel 1 goto DONE goto DELETE :JUNE dir %1 | find " 05-" if not errorlevel 1 goto DONE goto DELETE :JULY dir %1 | find " 06-" if not errorlevel 1 goto DONE goto DELETE :AUGUST dir %1 | find " 07-" if not errorlevel 1 goto DONE goto DELETE :SEPTEMBER dir %1 | find " 08-" if not errorlevel 1 goto DONE goto DELETE :OCTOBER dir %1 | find " 09-" if not errorlevel 1 goto DONE goto DELETE :NOVEMBER dir %1 | find " 10-" if not errorlevel 1 goto DONE goto DELETE :DECEMBER dir %1 | find " 11-" if not errorlevel 1 goto DONE goto DELETE :DELETE del %1 echo %1 ***** DELETED ***** :DONEWell, you see how long the batch file version is. Not very elegant. But yah, it DOES work...
The easiest way is to not use DOS! Visual Basic has built-in date commands and can do real math! Tell you what... Why don't you browse on over to my Windows Scripting page. Scroll down to the bottom under the "File Utilities" section and look at NoOldFiles.vbs and DeleteOldFiles.vbs. Way better.
Lost? Look at the site map.
Bad links? Questions? Send me mail.