-1

I have a folder named backup in D: drive, under that folder there is a daily backup getting created for a day with folder names as the date of the backup.

I only want to keep the backup data for yesterday, (i.e. the latest one) and delete the old ones on daily basis automatically.

The problem I'm facing is, the data under the latest folder also is getting deleted.

Could you please help me with a script which only removes the old folders except the one recently modified or modified yesterday, in such a way that it doesn't delete any data from the recent folder.

Here is the code I am using:

forfiles -p "D:Test" -s -m *.* /D -1 /C "cmd /c Del @path"
Compo
  • 36,585
  • 5
  • 27
  • 39
Ankush
  • 7
  • 4
  • 2
    Helping implies that you did somethin on your own, so please share your efforts! Check out these: [Batch file to delete files older than N days](https://stackoverflow.com/q/51054), [Batch file to delete folders older than 10 days in Windows 7](https://stackoverflow.com/q/5497211) – aschipfl Nov 28 '17 at 12:54
  • You have only provided a very high level overview of your problem. You have not provided even remotely enough technical specifications to even begin programming any code. – Squashman Nov 29 '17 at 04:16
  • Hi Squashman, So what I am trying to achieve is, lets consider : I have a folder named "backup" in D: drive, under that folder there is a daily backup getting created for a day with folder names as the date of the backup. So I only want to keep the backup data for yesterday (i.e. the latest one) and delete the old ones on daily basis automatically. So what problem I'm facing is, the data under the latest folder also is getting deleted. But what I want to do is only delete the old folder without affecting any files or folder under the latest folder. – Ankush Nov 29 '17 at 12:21
  • Hi aschipfl, I tried running this script - _forfiles -p "D:Test" -s -m *.* /D -1 /C "cmd /c Del @path"_ But what is happening is, it is also deleting the files under the latest backup folder which have a last modified date with more than one day – Ankush Nov 29 '17 at 16:17

2 Answers2

0

Check if file is directory and do remove dir rd if is older than 100 days.

forfiles /S /D -100 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

ForFiles /P "D:Test" /D -1 /C "CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE"

The above script worked fine.

Ankush
  • 7
  • 4