autodelete files older then a specific age (Batchfile)

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-04-13
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!
Question by:toffel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

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


Expert Comment

ID: 8218787
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


Author Comment

ID: 8218909
One month is also ok

Accepted Solution

billious earned 200 total points
ID: 8222150
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!!


Expert Comment

ID: 8595628
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 http://www.experts-exchange.com/Community_Support/ giving the address of this question. Thank you      


Cleanup Volunteer


Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question