?
Solved

Batch Script to Delete files from Folders and Sub Folders Older than 2 months old.

Posted on 2011-05-03
5
Medium Priority
?
3,173 Views
Last Modified: 2012-06-21
Hi! I was given the script below by billprew. My request was to be able to check folders and delete files older than 2 months.

I now have a question that's related to my intial question. I do need a script that deletes files that are older than 2 months old.

But I would need to script to delete files that are located in subfolders as well. For example, we have D:\Users.

Within the users directories, you would have many sub-folders and sub-sub folders.

I need help with a batch script that checks a specific drive, or folder and its sub folders and deletes files that are older than 1 month, or 2 months.



@echo off  
setlocal EnableDelayedExpansion  
 
REM Define base for folders, and days to keep old folders  
Set BaseDir=c:\videos  
set DaysToKeep=7  
 
REM Get todays date (MM/DD/YYYY), convert to julian for age checks  
call :jDate jToday %DATE:~-10%  
 
REM Process all Files in the directory, delete if old  
for %%A in ("%BaseDir%\*.*") do (  
  call :jDate jFile %%~tA  
  set /A FileAge = !jToday! - !jFile!  
  if !FileAge! GTR %DaysToKeep% (  
    ECHO File:[%%A] is [!FileAge!] days old and would be deleted  
    REM del "%%~A"  
  )  
)  
 
REM Done  
exit /b  
 
REM Subroutine to calculate julian date  
:jDate return-variable date-string(MM/DD/YYYY)  
  set DateStr=%~2  
  set yy=%DateStr:~6,4%  
  set /A mm=1%DateStr:~0,2%-100  
  set /A dd=1%DateStr:~3,2%-100  
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"  
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4  
  exit /b
0
Comment
Question by:ben1211
5 Comments
 
LVL 4

Assisted Solution

by:pritamsh
pritamsh earned 200 total points
ID: 35664745
use forfiles command;

forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"

Reference : http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 35688181
Aside from the scripts you have, there is one I have here which is a VBScript but you can soon call it from batch file or command prompt with:

cscript //nologo delolder.vbs

Save it as delolder.vbs and then amend the line:

DeleteFiles "c:\temp",DateAdd("m", -1, Date)

to be the path you want, and the DateAdd command currently looks for files 1 month old from today... change that to 2 for 2 months, "y",-1 for 1 year old etc.

It will remove any already, or now, empty directories created before the same date too.

' http://scripts.dragon-it.co.uk/links/batch-delete-files-older-than
' Call initial folder to delete and it will work down subdirs
' By running recursively through subdirs after doing files
' in each dir. Second parameter is date of last files to leave
' Anything older will go.
' Stephen Knight July 2009

Dim deletionDate
Dim fso
Dim oFile
Dim oFolder

Set fso = CreateObject("Scripting.FileSystemObject")

DeleteFiles "c:\temp",DateAdd("m", -1, Date)

msgbox "Deleting older than " & DateAdd("m", -1, Date)

Set oFolder = Nothing
Set oFile = Nothing
Set fso = Nothing

Function DeleteFiles(foldername,cutoffdate)
Set oFolder = fso.GetFolder(foldername)

For Each oFile in oFolder.Files
If ofile.DateCreated < cutoffDate Then
fso.DeleteFile oFile, True
End If
Next

For Each subFolder In oFolder.SubFolders
DeleteFiles subFolder.Path, cutoffdate
If (SubFolder.Files.Count = 0) and (SubFolder.SubFolders.Count=0) And (SubFolder.DateCreated < cutoffdate) Then
msgbox "deleting " & subfolder.name & " created on " & subfolder.DateCreated
call SubFolder.Delete()
End If
Next

End Function
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1800 total points
ID: 35689082
This simple one line change should handle it.

@echo off
setlocal EnableDelayedExpansion

REM Define base for folders, and days to keep old folders
Set BaseDir=c:\videos
set DaysToKeep=100

REM Get todays date (MM/DD/YYYY), convert to julian for age checks
call :jDate jToday %DATE:~-10%

REM Process all Files in the directory, delete if old
for /R "%BaseDir%" %%A in ("*.*") do (
  call :jDate jFile %%~tA
  set /A FileAge = !jToday! - !jFile!
  if !FileAge! GTR %DaysToKeep% (
    ECHO File:[%%A] is [!FileAge!] days old and would be deleted
    REM del "%%~A"
  )
)

REM Done
exit /b

REM Subroutine to calculate julian date
:jDate return-variable date-string(MM/DD/YYYY)
  set DateStr=%~2
  set yy=%DateStr:~6,4%
  set /A mm=1%DateStr:~0,2%-100
  set /A dd=1%DateStr:~3,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b

Open in new window

~bp
0
 

Author Comment

by:ben1211
ID: 35744795
Bill where did you make the change please?
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1800 total points
ID: 35753367
for %%A in ("%BaseDir%\*.*") do (  

was changed to

for /R "%BaseDir%" %%A in ("*.*") do (

~bp
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

864 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