autodelete files older then a specific age (Batchfile)

I have a shared directory in a W2K network. I would like to configure that the files in this folder are  automatically deleted 30 days after tey have been saved there. Is this possible in a batchfile, or should I consider other solutions (like perhaps in the W2K resource kit)?
Thanks in advance!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It's relatively simple using batch, but are you stock on "30 days" - would "1 month" be preferable?

I've written a script in VBS to do such a thing.  It takes parameters of directory in which to delete and the number days old the file needs to be to be deleted.

It's fairly straight forward in VBS using the FileSystemObject, though I've found some anti-virus software disables this, making the script useless.

Anyway, here's the code.

Set fso = CreateObject("Scripting.FileSystemObject")

Set oArgs = wscript.Arguments

if oArgs.Count<>2 then
  wscript.echo "Usage <script> <dir> <days old>"
  iDays = oArgs(1)
  szDir = oArgs(0)
  if not IsNumeric(iDays) then
    wscript.echo("<days> must be numeric.")
  end if
  if not fso.FolderExists(szDir) then
    wscript.echo("No such directory " & szDir)
  end if
end if

Set dir = fso.GetFolder(szDir)
Set files = dir.Files

for each file in files
  if DateDiff("d", file.DateLastModified, Now)>CInt(iDays) then
    wscript.Echo(szDir & file.Name & " deleted.")
    file.Delete true
  end if

Set fso = Nothing

toffelAuthor Commented:
One month is also ok
Since 'find the date xx days before ...' is such a common DOS-BATCH question, I've written a routine to do it:


:: set dd, mm, yyyy to today - n or supplied-date - n
:: dd, mm in leading-zero-filled format
:: first parameter is number of days, n - default is 1
:: second parameter is date

@echo off
set daysoff=%1
if "%daysoff%"=="" set daysoff=1
SET thedate=%2
IF not "%thedate%"=="" goto datespecd
for /f %%i in ('date /t') do set thedate=%%i

:: for dd/mm/yy format - swap "dd" and "mm" in following 2 lines
set /a mm=%thedate:~0,1% * 10 + %thedate:~1,1%
set /a dd=%thedate:~3,1% * 10 + %thedate:~4,1%
set /a yyyy=%thedate:~6,4%

:: try to sensibly fill out 2-digit years
if %yyyy% lss 80 set /a yyyy=2000+%yyyy%
if %yyyy% lss 100 set /a yyyy=1900+%yyyy%

if /i %daysoff%==0 goto final
set /a daysoff=daysoff - 1

set /A dd=%dd% - 1
if %dd% GTR 0 goto DONE
:: else last day of month - assume 31
set dd=31
set /A mm=%mm% - 1
if %mm% GTR 0 goto SETDAY
:: Must be Dec. 31st
set /A mm=12
set /A yyyy=%yyyy% - 1
goto DONE

::Problem months are Feb, Apr,Jun,Sep and Nov
if %mm%==4 goto SET30
if %mm%==6 goto SET30
if %mm%==9 goto SET30
if %mm%==11 goto SET30
if not %mm%==2 goto DONE
:: February - default to 28 days (exception for leap years)
set /A dd=%yyyy% %% 4
if not %dd%==0 goto set28
set /A dd=%yyyy% %% 100
if not %dd%==0 goto set29
set /A dd=%yyyy% %% 400
if not %dd%==0 goto set28

set dd=29
goto done

set dd=28

if /i %daysoff%==0 goto final
if %dd% gtr %daysoff% goto thismonth
set /a daysoff=%daysoff% - %dd%
set dd=1
goto set30

set /a dd=%dd% - %daysoff%

:: now convert back to 2-digit dd and mm
for %%i in (thedate daysoff) do set %%i=
for %%i in (1 2 3 4 5 6 7 8 9) do call :lz %%i

:: prefix %dd%, %mm% with leading zero
if %dd%==%1 set dd=0%dd%
if %mm%==%1 set mm=0%mm%
goto :eof



And if this above is in your path as 'daysb4.bat', then the following should do your task:


:: move files dated before %1 days
:: uses daysb4.bat
:: first parameter is directory
:: second parameter is Destination directory
:: third parameter is filemask - default is *.*
:: fourth parameter is number of days, n - default is 30

@echo off
if [%2]==[] for %%i in (echo goto) do %%i end - syntax is moveb4 source dest [days]
set days=%4
if "%days%"=="" set days=30
set destdir=%2
call daysb4 %days%
set days=%yyyy%%mm%%dd%
pushd %1
set dd=%3
if [%3]==[] set dd=*.*
if not exist %dd% goto nomoves
for /f %%i in ('dir /b %3') do (
:: change %%j%%k to %%k%%j for dd/mm/yy format
for /f "tokens=1-3 delims=/ " %%j in ('dir %%i^|find "/"') do call :moveme "%%i" %%l%%j%%k
for %%i in (destdir filename days dd mm yyyy) do set %%i=
GOTO end

set yyyy=19%2
if %yyyy% lss 19800000 set /a yyyy=20%2
:: remove the 'echo' below to do the move
if %yyyy% lss %days% echo move %1 %2

goto :eof



1) set up for mm/dd/yy format - follow internal documentation to change to dd/mm/yy
2) the second script will merely echo the command it would perform - remove the echo after thorough testing!!


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This question is still open and getting old. If any of the comment(s) above helped you please accept it as an answer or split the points who ever helped you in this question. Your attention in finalising this question is very much appreciated. Thanks in advance,


- If you would like to close this question and have your points refunded, please post a question in community support area on giving the address of this question. Thank you      


Cleanup Volunteer

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 2000

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.