ForFiles not working for recursive folder with dynamic folder name

2.5k Views Asked by At

I wanna use forfiles to delete files older than 92 days, but the code seems to be breaking. I have a dynamic folder name.

for /d %X in (e:\local\test\backups\s?????pbx\) do forfiles /p %X /m *.cab /c "cmd /c del @path" /d -92

I couldn't figure out where I am going wrong. Is there any other way to achieve this? Any help would be appreciated.

2

There are 2 best solutions below

0
On BEST ANSWER

Windows commands only support wildcards in the terminal node of a path - It will not find paths that appear anywhere before the last \.

The solution for your problem is really simple :-) Just remove the final \

for /d %X in (e:\local\test\backups\s?????pbx) do forfiles /p %X /m *.cab /c "cmd /c del @path" /d -92
4
On

Batch uses * as a wildcard character. Try this:

for /d %X in (e:\local\test\backups\s*pbx\) do forfiles /p %X /m *.cab /c "cmd /c del @path" /d -92