Avatar of Brian
Flag for United States of America

asked on 

Batch file to delete files older than 30 days, but a minimum of 10 files

I have a batch file that contains the following command to delete files older than 30 days.
forfiles /P D:\backups\configFiles\ /S /M *.tar /D -30 /C "cmd /c del @PATH"

Open in new window

But I want to only delete files if there are more than 10 files in the folder.  I found this code for counting the files, but can't get it to work.
for /f %%A in ('dir ^| find "File(s)"') do set cnt=%%A

Open in new window

Can someone show me what I need to add to my batch file to keep the delete command from deleting if there are less than 11 files in the folder.  In other words, if there are 10 files or less, then I want it to skip the deletion.
Windows BatchPowershell

Avatar of undefined
Last Comment

8/22/2022 - Mon