# Batch file deletes old folders but w/errors



## Drewzr (Aug 22, 2013)

I created a batch file to delete folders older than 30 days. It works perfectly but with an error.. it will delete say 5 folders then there will be 5 lines of "ERROR: The system cannot find the file specified"

Here is my code:

ForFiles /P c:\folder\folder2 /s /D -30 /C "CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE"

I am guessing the error is from the two "/s" in the code? What I need to do is find and delete all directories and subdirectories within the directory "Folder2" that are older than 30 days.

Any suggestions on where I went wrong? I am assuming the error is harmless but I would like to know why it is being generated.

Many Thanks!

Drew


----------



## foxidrive (Oct 20, 2012)

Try enclosing this part in parentheses. 

(echo RD /Q @FILE &RD /Q /S @FILE)


----------



## Drewzr (Aug 22, 2013)

Unfortunately that didn't help..


----------



## foxidrive (Oct 20, 2012)

It was recursing into a tree after the top level folder had already been removed. So removing the /s from before the /d stopped it recursing.


```
ForFiles /P d:\folder /D -30 /C "CMD /C if @ISDIR==TRUE echo RD /Q @path & if exist @path\ RD /Q /S @path"
```


----------



## Drewzr (Aug 22, 2013)

I see what you are saying and your suggestion does work. However, I get an error message that nothing was found because (I think) it is not searching sub directories. If I put the /s after my path it will find the sub directories. The folders I want to delete are three directories deep with multiple directories under the top directory. What if I removed the second /s in the code?


----------



## foxidrive (Oct 20, 2012)

You have shown the original folder as *c:\folder\folder2 
*so if the folders you need to remove are

*c:\folder\folder2\apple
c:\folder\folder2\apple\toast
c:\folder\folder2\apple\toast\coffee

*Then all you need to deleteis the apple folder.

What you actually need to delete is unclear...


----------



## Drewzr (Aug 22, 2013)

Sorry, I suppose that would be helpful.. I have a program that creates and saves data into a directory.
as an example

c:\Employee\Building\Smith\08-25-2013

The program creates a directory every day and labels it with the current date and stores it under employees last name ("Smith" in example above). There are multiple last name directories and I want to limit the date directories for each of them to 30 days or less. I can't do the path simply as c:\Employee\Building\Smith because I would have to do that for all 40 last name directories. So I want the path c:\Employee\Building\ and search all the "last name" directories for "date directories" 30 days and older and delete them.

Hope that makes a little more sense..


----------



## foxidrive (Oct 20, 2012)

This is untested but should check every folder in "c:\Employee\Building" which are the employee surnames, and then use the forfiles command with each of those folders.

Test it on some sample folders first.


```
@echo off
pushd "c:\Employee\Building"
for /f "delims=" %%a in (' dir /b /ad ') do (
ForFiles /P "c:\Employee\Building\%%a" /D -30 /C "CMD /C if @ISDIR==TRUE echo RD /Q /S @FILE &RD /Q /S @FILE"
)
popd
```


----------

