Script for automated Purging / Deleting files on your server to save space

This post is about showing you how you can put together a quick script to purge specific files on your computer/server so that you don't run out of space.

This is something that you would use most likely on your servers where your websites or your applications are logging things into specific folders. You can spend money and buy some enterprise level application that can track the space on your servers and purge necessary files, but you can also implement the following scripts for free.

The following script uses the following Windows commands:
  • forfiles
  • del
  • call
  • setlocal
  • endlocal
  • echo
  • set
  • MyEmailCmd.exe  (a little exe that put together for sending emails and I use in my scripts)

If you want to just write a quick script to purge/delete files in a specific folder that are older than certain number of days, you can just do this:

forfiles /p C:\temp /m *.txt  /d -90 /c "cmd /c del /f @path"

This one command-line will delete any txt file older than 90 days from C:\temp folder. You can set up scheduled task on your server to call this script once a day and that will keep your server clean. This will do the job.

What if you wanted to make this a bit more configurable? What if you wanted know what files are purged and if you wanted to get notified via email on what files got purged every day so you have a bit more visibility what is going on the server?

Then you can implement in the following way:

I decided to set up the following files:
  • ConfigFile.bat  (Script that acts as a configuration file. This is only file that somebody maintaining these scripts needs to understand)
  • InvokePurgeFiles.bat  (High-level script that just invokes the main script and performs certain actions when the main scripts is done executing)
  • PurgeFiles.bat   (Main script that does the main work)
  • PurgeSpecificFile.bat  (Low-level script that is called by the main script to perform certain actions)

How do you run this?
  • Put these script files in a specific location on your server (i.e.  C:\Script\PurgeScripts)
  • Adjust the parameters ConfigFile.bat to what what you need.
  • Set up a scheduled task to call InvokePurgeFiles.bat  script which is the high-level script.

Here are the scripts.


ConfigFile.bat
____________________________________________________________
@echo off

set WORKING_DIR="C:\ThePurgeScripts"
set PURGE_DIR="C:\temp"
set PURGE_FILES_OLDER_THAN_X_DAYS=90
set PURGE_FILE_SEARCHMASK="*.txt"
set NAME_OF_COMPONENT_BEING_TRACKED="My Web Server"
set PURGE_OUTPUT_FILE=PurgeOutput.txt
____________________________________________________________




InvokePurgeFiles.bat
____________________________________________________________
@echo off

SETLOCAL

REM Set up the configuration
call ConfigFile.bat

REM Call the main script that does all the main work
call PurgeFiles.bat > %WORKING_DIR%\%PURGE_OUTPUT_FILE%

REM Email the output the purge to yourself so that you know what files got purged
MyEmailCmd.exe -smtp smtp.yourSMTPServer.com -s "%NAME_OF_COMPONENT_BEING_TRACKED% (%date% %time%) (List of files that got purged)" -from you@yourDomain.com -to you@yourDomain.com -body "" -attach %WORKING_DIR%\%PURGE_OUTPUT_FILE%

ENDLOCAL
____________________________________________________________



PurgeFiles.bat
____________________________________________________________
@echo off

REM Set up the configuration
call ConfigFile.bat

REM Using forfiles command find all the files that need to be deleted and call PurgeSpecificFile.bat for each file that meets that criteria
forfiles /p %PURGE_DIR% /m %PURGE_FILE_SEARCHMASK% /d -%PURGE_FILES_OLDER_THAN_X_DAYS% /c "cmd /c call %WORKING_DIR%\PurgeSpecificFile.bat @path"
____________________________________________________________



PurgeSpecificFile.bat
____________________________________________________________
@echo off

set THEFILE=%1

echo Deleting the file %THEFILE% 
del /f %THEFILE%
____________________________________________________________



Before you start using these scripts here are few important things that you need to know:
  • If you are not sure if you want to start the purging and you want to visualize what files the script would actually delete, you can run it in such way so it is NOT deleting files and it is just echoing what it would delete. You can just go into the script PurgeSpecificFile.bat and comment out the line that executes the deleting. Then this whole package would just be pretending it is deleting the files and it would just echo the file and save it path into the %PURGE_OUTPUT_FILE
  • If you want to utilize my little exe to send emails, you can download these scripts and the tool from the following location: Download link to my Google Drive

Enjoy !



- almirsCorner.com -

#batch #script #batchscript #forfiles #commandline #command #dos #doscommands #purgefiles #delete #tech #scripting #programming #devops 


No comments:

Post a Comment