0

Hi I need to delete folders on my Windows 7 computer which are older than 7 days and have data in it.

I have used the following commands which haven't worked out well for me

  1. FORFILES /S /D -10 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"
  2. forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"

The second command which I referred from Batch file to delete files older than N days also didn't work for me.

Can someone please suggest what to use in the second command on C:\what\ever or suggest an alternative command to delete my folders older than 7 days.

Community
  • 1
  • 1
sharoon d'cunha
  • 47
  • 1
  • 3
  • 9
  • 1
    Well, second command as posted is for deleting files older than 7 days and not for directories. So this could not work for the task deleting folders being older than 7 days. I must add that I'm not sure how you want to detect which subfolder is older than 7 days: by checking if last modification date of all files in the subfolder and all its subfolders is older than 7 days or by just looking on date of subfolder. This could make a big difference. – Mofi Apr 21 '16 at 05:49
  • Sorry for the inconvenience. i have re-framed my subject now. to get a clear picture what I want ,I will just explain you this way- I have a parent folder for eg: backup which has all the subfolders in zipped format arranged according to their dates. i need to delete all the subfolders which are older than 7 days . for instance if today's date is : 21st April , I want all sub-folders to be deleted which were created before 14th April . The name of my folder goes in following series - 2016-03-10_1646(yyyy-mm-dd_hhmm). I hope this is clear now . – sharoon d'cunha Apr 21 '16 at 06:43
  • In the second command I have replaced arguments appropriately. In addition to this I have also tried more than 5 to 6 ways . My only doubt in that command "forfiles -p "C:\what\ever" -s -m *.* -d -c "cmd /c rd @path" was- whats the diffference between @path and "C:\what\ever" ? – sharoon d'cunha Apr 21 '16 at 06:48

2 Answers2

0

So your task is to delete older backup directories no longer needed.

The tricky solution would be working with "delete older than X days".

The simple solution is following:

@echo off
set "BackupDirectory=C:\Backup"
for /F "skip=7 delims=" %%D in ('dir "%BackupDirectory%" /AD /B /O-D 2^>nul') do (
    rd /Q /S "%BackupDirectory%\%%D"
)

The command DIR returns a list which contains

  • because of /AD (attribute directory) just the subdirectories and
  • because of /B (bare format) just directory names and
  • because of /O-D ordered by date with newest at top and oldest last.

The command FOR skips the first 7 lines from list, i.e. the 7 newest subdirectories, and executes on the other (older) directories the command to remove the subdirectory.

In your case with directory names starting with yyyy-mm-dd it would be also possible to use /O-N (ordered reverse by name) instead of /O-D to keep skip=x newest subdirectories and delete all others.

Note: On NTFS partitions the last modification date of a folder changes if any file/folder in this folder is added/modified/deleted, but not on FAT16, FAT32 or exFAT partitions.

On DIR command line after /O-D the option /TC can be added get output the directory list sorted reverse by creation date of the folders. But backup folders are usually not modified later after backup was created. It is a matter of opinion to keep the newest folders taking modifications in the folders into account or rate only folder creation time.

In my experience on deleting backups the date is not so important. Important is only limiting the number of backups to avoid filling a storage media. For example if a backup usually needs 5 GiB, the number of backups to keep might be 10 or 20, but if a backup usually needs only 500 KiB, the number could be increased to 100. The date does not matter, just the total amount of bytes required for the backups, as the limit is the storage media size and not the time span.

And for a log file with lines always appended on each execution of a backup operation, the size of the log file must be usually observed and not the time span on which lines are appended to same log file to avoid that the log file becomes larger and larger. Moving a log file with size > x KiB or MiB to *_old.log with /Y to overwrite an already existing *_old.log and then redirect the new lines into a new log file is quite often the right strategy to have just 2 logs files (*.log and *_old.log) with a defined maximum file size containing log lines of the last x backup operations.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

Remove old backup folders older than /d days. So use a minus value.

Oneliner...

forfiles /p y:\foldername /d -50 /c "cmd /c if @isdir==TRUE rmdir @file /s /q"

You want to leave off the forfiles "/s" if you only want to scan the top level folders and once you have that folder, you rmdir the entire folder.

The reason for using rmdir is because if you use del or erase, once it deletes a file, it can change the date on the holding folder. Then the command leaves folders behind.

/p - for path
/d - minus for 50 days old
/c - run a command
@isdir - if folder
Rmdir - better than del or erase
@file - what it was looking for
/s - subfolders
/q - quiet
Bendo
  • 41
  • 3