2011/02/04

How to delete files older than X days in Windows Batch

In this entry I will explain how to delete files in a Windows environment depending on its age.

This task is rather easy in Unix but in Windows (DOS) it gets quite more complicated.

Now imagine you have some folders storing all the backups the system does, and now you would like to clean those folders so they just have the last ones.

I searched a lot and seems that with normal Batch is almost impossible to do it, and after trying like 3 or 4 different options I found this one to be the best overall.

First off, as I already said, with normal batch this task is so complicated, that is why we will use and extra tool called Robocopy. This tool is included in the Windows Server 2003 Resource Kit Tools which you can download from here:

http://download.microsoft.com/download/8/e/c/8ec3a7d8-05b4-440a-a71e-ca3ee25fe057/rktools.exe

Robocopy is a powerful copy command with lots of useful options.

Then idea is quite simple:

- We will create a temporal folder.
- We will move the files we want to delete to that folder.
- Finally we will delete the folder created with the files in it.

Now on to the code.

I will just put the code commenting each line, instead of writing what it does in a not-so-easy-to-understand paragraph.

* * * * *

@ECHO OFF
ECHO # Starting...
ECHO.

REM Setting the log file name
SET day=%date:~0,2%
SET mon=%date:~3,2%
SET year=%date:~6,4%
SET logfile_name=clearing_log_%day%-%mon%-%year%

REM Writing the first line in the log including the time
SET time=%time:~0,5%
ECHO # Clearing.bat launched at %time% > %logfile_name%.txt
ECHO. >> %logfile_name%.txt

REM Program Start

REM variables - here just write the paths of the folders you want to clear
SET folder_1_path="C:\Documents and Settings\user\Desktop\Folder_1"
SET folder_2_path="C:\Documents and Settings\user\Desktop\Folder_2"

REM this next variable will be the path of the temporal folder (you can write whatever path you want as it will be deleted)
SET path_temp_folder="C:\Documents and Settings\user\Desktop\Justin Bieber"

REM first we will check if the folders specified in the path variables exists
IF EXIST %folder_1_path% (
GOTO folder_1_exists
) ELSE (
ECHO [ERROR] The folder 1 does not exist.
ECHO [ERROR] The folder 1 does not exist. >> %logfile_name%.txt
GOTO final
)

:folder_1_exists
REM now we check if the second folder exists
IF EXIST %folder_2_path% (
GOTO folder_2_exists
) ELSE (
ECHO [ERROR] The folder 2 does not exist.
ECHO [ERROR] The folder 2 does not exist. >> %logfile_name%.txt
GOTO final
)

:folder_2_exists
REM now we will delete the files in the first folder

REM first we create the temporal folder
MKDIR %path_temp_folder%

REM now we move the files older than X (in this case 7) days to that folder
ROBOCOPY %folder_1_path% %path_temp_folder% /move /minage:7 >nul
REM ROBOCOPY syntax = source destination [file [file]…] [options]

REM and finally as stated we delete the temporal folder
RMDIR %path_temp_folder% /s /q

ECHO Folder 1 cleaned.
ECHO Folder 1 cleaned. >> %logfile_name%.txt

GOTO clear_1_done

:clear_1_done
REM now we will delete the files in the second folder

REM first we create the temporal folder
MKDIR %path_temp_folder%

REM now we move the files older than X (in this second case 3) days to that folder
ROBOCOPY %folder_2_path% %path_temp_folder% /move /minage:3 >nul
REM ROBOCOPY syntax = source destination [file [file]…] [options]

REM and finally as stated we delete the temporal folder
RMDIR %path_temp_folder% /s /q

ECHO Folder 2 cleaned.
ECHO Folder 2 cleaned. >> %logfile_name%.txt

GOTO final

:final
ECHO.
ECHO # Done.
ECHO. >> %logfile_name%.txt
ECHO # Done. >> %logfile_name%.txt
ECHO # Log file = %logfile_name%.txt
ECHO # Press any key to exit.
PAUSE>NUL

* * * * *

That's it.
Just create and new .bat file, copy the code above, modify it (paths, older days...etc) to match your goals and voilĂ  ;)

Hope it helps.

No comments:

Post a Comment