0

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
Aarie
  • 39
  • 7
  • I can see no reason to run a script every three minutes or even once per day when removing directories whose contents have not been modified in the previous four months! – Compo Sep 01 '18 at 16:20

2 Answers2

1

I propose a two-step solution:
1- move files older than x days to a temporary folder
2- delete that folder completely

@echo off

pushd \\networkpath\backupdirectory

REM move old files to temp folder
set tmp=%RANDOM%.tmp
md %tmp%
robocopy . %tmp% /E /MOVE /XD %tmp% /minage:120 /R:1 /W:1

REM delete temp folder
set empty=%RANDOM%.tmp
md %empty%
robocopy %empty% %tmp% /E /purge

rd %tmp%
rd %empty%

This does not look at the folders' timestamps at all; instead, it looks at the files' ages, recursively in all existing folders. If a folder contains both old and new files the script will not delete that folder but only the old files within.
The age is given in days.

What I really wanted to do is move the selected files to NUL. Alas, robocopy doesn't recognize NUL as a valid path even though it is defined in the registry.

Performance might be an issue here - if the files to be deleted are first moved across the host from which the command is started then it might take a long time. In this case, maybe the script can be scheduled to run on the server itself.

user1016274
  • 4,071
  • 1
  • 23
  • 19
0

Here is a hybrid script that you can set the number of days. You need to change the day=-120 to the relevant number of days you want. It does not take into account the time, so use your scheduler to run at a time of day.

@echo off
set day=-120
pushd \\networkpath\backupdirectory
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\*%~n0.vbs"
set "yyyy=%result:~0,4%"
set "mm=%result:~4,2%"
set "dd=%result:~6,2%"
set "final=%yyyy%%mm%%dd%"
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir "BKP_*" /a:d /o:-d /b') do (
set "folder=%%a"
set folddate=!folder:~4,8!
if !folddate! LSS !final! call :work
)
goto :EOF

:work
echo !folder! >> D:\data\logs\Log.txt
rmdir emptyfolder
mkdir emptyfolder
robocopy emptyfolder "!folder!" /purge                  
rmdir !folder!
rmdir emptyfolder
Gerhard
  • 22,678
  • 7
  • 27
  • 43