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:
- 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.
set NAME_OF_COMPONENT_BEING_TRACKED="My Web Server"
REM Set up the configuration
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%
REM Set up the configuration
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"
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
- almirsCorner.com -
#batch #script #batchscript #forfiles #commandline #command #dos #doscommands #purgefiles #delete #tech #scripting #programming #devops