I am looking for a way of deleting files that are contained in a folder with a specific name and then delete that folder afterwards.
I know there there is the forfiles command line that I can use to delete the files, I just don't know how to get the folder name into the path. The folder is named based on the date that it was created and I want to delete the folder that is 7 days old. The folder is formatted as yyyymmdd.
basically I need
forfiles /P "C:\backups\[DATE 7 DAYS AGO IN yyyymmdd format]" /S /M *.BAK /C "cmd /c del @path"
and then I think to delete the folder after it would be
forfiles /P "C:\backups\[DATE 7 DAYS AGO IN yyyymmdd format]" /S /C "cmd /c IF @isDir == TRUE rd @path"
How do I fill in the [DATE 7 DAYS AGO ...] for these.
Alright so I followed Elektro Hacker's advice and I ended up doing this via powershell, which looks as follows:
In this I have 3 different backups that occur (daily, weekly, and monthly). So basically I delete all the files from daily that are a week old, weekly that are a month old and monthly that are a year old. I also print to a file the date and folder that was removed for each, or a failure message in the event that the folder still exists or and exception has occurred. This script is then run daily and is working great so far.
Thanks, DMan
Try using
rmdir
(at the command prompt typermdir /?
for help)1st - why you try to delete first the files, and next the tolfer? If you delete the folder first you are doing the same thing in less time...
2nd - I THINK You can't do that in Batch, not without errors, you need to extract (know) the calendar days for at least the "X" years you need to use in your purposes, and Batch don't have any way to extract that info, or to be a math genious to know how to take the calendar dates of past years.
so you need a realistic scripting language who can acces to the Microsoft.Time and data library and do the comparision. (Ruby, Python, Perl, all what you want less Batch)
By the way exist some ultra advanced aricmetics batfile codes wrote by RobVanDeerWoud to "play" with things, for example with this script you can add "X" days to a date, take a look to see if you can modify to deduct days:
(CREDITS FOR THE AUTHOR OF CODE: ROBVANDERWOUD)