Link to home
Start Free TrialLog in
Avatar of Stuart Oram
Stuart OramFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Delete directory using date variable

We are currently using a batch file that runs a 'mkdir' using a pre-defined variable and then calls another batch file to run a backup task, to supplement our existing tape backup for a short period of time.

setlocal enableextensions
set name=%DATE:/=_%
mkdir G:\Backup\%name%
call C:\FFSBackup.bat

Open in new window

This produces folder names in the format 20_06_2018

I would like to be able to have another scheduled task to delete historical directories - the above runs daily (Mon-Fri) through a scheduled task and I would like to be able to remove directories that are, for example, older than 5 days to manage space on the destination disk - a USB drive. Is there any way I can use the above date variable to achieve this?
ASKER CERTIFIED SOLUTION
Avatar of Coralon
Coralon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stuart Oram

ASKER

Thanks very much. I'm very much a novice with Powershell, but I have picked up a number of scripts from here and elsewhere recently for other things, so I will give this a go and update in due course!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh and have a look at my article on dates in batch files -- https://www.experts-exchange.com/articles/1153/Using-dates-in-batch-files-scripts.html which explains better ways than using %DATE% as that is affected by user date formats, i.e. if you scheduled a job and it runs as a different user it might end up being mm-dd-yyyy or dd/mm/yyyy etc.

Another way to age files in batch/VBScript here:  http://scripts.dragon-it.co.uk/links/batch-delete-files-older-than
http://scripts.dragon-it.co.uk/links/batch-versioning-backup

Steve
Thanks both - need to give it a few days as we recently manually had a clear out! Once I've got some older folders, I'll test out the above.