Over a network path, two huge sized folders are added every day with naming format BKP_YYYYMMDDhhmm . When we try to manually delete those folders for housekeeping, it throws well known error:
SOURCE PATH TOO LONG
So I created below housekeeping batch to delete those stubborn folders using ROBOCOPY route selecting the oldest folder as an input at a time.
I have scheduled this batch to run on every 3 mins and it’s working fine. But I want this batch to delete only till those folders which are older than 4 months.
I found many solutions to achieve this but can’t leave this ROBOCOPY route due to above mentioned ‘SOURCE PATH TOO LONG’ error occurring for all folders.
Could you please guide me what modification I need to do in below script sothat it can be scheduled to run at a fixed time in a day to delete folders older than 4 months only?
Thanks in advance!
@echo off
pushd \\networkpath\backupdirectory
for /f "delims=" %%a in ('dir "BKP_*" /a:d /o:-d /b') do set "folder=%%a"
echo %folder% >> D:\data\logs\Log.txt
rmdir emptyfolder
mkdir emptyfolder
robocopy emptyfolder "%folder%" /purge
rmdir %folder%
rmdir emptyfolder
exit