Stuart Oram
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.
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?
setlocal enableextensions
set name=%DATE:/=_%
mkdir G:\Backup\%name%
call C:\FFSBackup.bat
This produces folder names in the format 20_06_2018I 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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.
ASKER