ben1211
asked on
Batch Script to Delete files from Folders and Sub Folders Older than 2 months old.
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)/1 2)/4+367*( mm-2-(mm-1 4)/12*12)/ 12-3*((yy+ 4900+(mm-1 4)/12)/100 )/4
exit /b
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
exit /b
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bill where did you make the change please?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Fi
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,cut
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.Coun
msgbox "deleting " & subfolder.name & " created on " & subfolder.DateCreated
call SubFolder.Delete()
End If
Next
End Function