# another delete folders by date



## Nnyan (Aug 7, 2000)

Hello all,

Noob here and to start off I'll say that I've search these forums and I have run into a few posts about this topic (search function kept giving error) but nothing that answers my question exactly.

I'm currently running some batch scripts in windows command prompt that delete files that are older the X date using FOREFILES (EX):

forfiles /P "PATH TO FILES" /M *.txt /D -7 /C "CMD /C if @ISDIR==FALSE echo del @FILE &del @FILE"

This works great for files but I need something simple like this for directories. I don't need exact math so if it's a day or so out of whack no big deal. I just can't seem to get anything like this to work for folders.

Any ideas?

Thank you in advance.


----------



## TheOutcaste (Aug 8, 2007)

```
ForFiles /P "PATH TO FILES"  /D -7 /C "CMD /C if @ISDIR==TRUE echo RD /Q /S @FILE &RD /Q /S @FILE"
```
Note that this will delete the contents of all folders and all subfolders in the Specified Path. Remove the /Q to be prompted for each folder.
If the folders are empty, the /S and /Q is not needed. Leave those switches off to avoid deleting folders that are not empty.


----------



## Nnyan (Aug 7, 2000)

awesome I'll give this a try and report back, that's exactly what I was looking for! Thank you!!!


----------



## Nnyan (Aug 7, 2000)

works perfectly. Forgot one thing in my orignal post. Is there anyway that I can use forefiles to do a conditional check? What I mean is IF there are files that are more then (ex) 7 days old do one thing (proceed to delete them) if there are no files 7 days or older then do something else (like GOTO :NOFILES)?

Thank you very much for your time!


----------



## TheOutcaste (Aug 8, 2007)

You might be able to just check the ERRORLEVEL after running the Forfiles command. If no files meet the criteria, it sets ERRORLEVEL to 1 and displays this message:
ERROR: No files found with the specified search criteria.

I don't know what other situations might set the ERRORLEVEL to 1 (or any other value) though, so it might not always work.

I tried an illegal command by renaming a file with a : in the name.
It displays Access Denied, But ERRORLEVEL is set to 0, so it may only get set to 1 if no files are found.

So this might work. The 2>Nul prevents the Forfiles error message from being displayed:

```
@Echo Off
SetLocal
Set _Path=C:\tstfldr1
ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File & Del @File" 2>Nul
If %Errorlevel%==1 Goto _NoFiles
Echo Files were deleted
Goto :EOF
:_NoFiles
Echo No files were found to delete
```
The other option is to first check to see if there are files that meet the criteria:

```
@Echo Off
SetLocal
Set _Path=C:\tstfldr1
For /F "tokens=1 delims=" %%I In ('ForFiles /P "%_Path%" /D -7 2^>^&1') Do Set _Result=%%I
If "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles
ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File & Del @File" 2>Nul
Echo Files were deleted
Goto :EOF
:_NoFiles
Echo No files were found to delete
```
*EDIT: *Tried a couple other commands like "%ComSpec% /C DIR G:\I don't Exist" and "%ComSpec% /C notarealcommand" and the Errorlevel returned is still 0. It does display the appropriate error message though
*The system cannot find the path specified.*
and
*'notarealcommand' is not recognized as an internal or external command,
operable program or batch file.*
If there is a syntax error (say you forget the /C switch before the command) it will also set errorlevel to 1
So it looks like the errorlevel will be 1 only if no matching files or found, or there is a syntax error


----------



## Nnyan (Aug 7, 2000)

I like the 2nd option to check for the files (since that is what I initially wanted to do). When I run that script even when is SHOULD it does not goto the :_NoFiles I just keep getting the files deleted message (with nothing listed).

I played around with this a bit and tried things like " IF ERRORLEVEL=1 GOTO :NOFILES " but no go. Ideally I would just want a check that if the condition is true (or false is find I can work with that) EX: there are folders older then the specified age, then do the delete function. If not then just GOTO.


----------



## TheOutcaste (Aug 8, 2007)

You can try this to see just what it is finding:

```
SetLocal
Set _Path=C:\tstfldr1
For /F "tokens=1 delims=" %%I In ('ForFiles /P "%_Path%" /D -7') Do Echo Loop Variable is ==%%I==
```
This will display the files/folders ForFiles finds, or display the error message. In this case, the error message will not be displayed as the Loop Variable. You'd see the error message, then *Loop Variable is ====*
Could be something as simple as your system doesn't have the period at the end of the error message.
The IF statement is also case sensitive. You can add the /I switch to eliminate that as a possibility:

```
If [COLOR=Red]/I[/COLOR] "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles
```
And make sure the labels match. They are not case sensitive, but Goto NoFiles is not the same as Goto _NoFIles


----------



## Nnyan (Aug 7, 2000)

When I run the first one I get what looks to be a listing of all the files in the folder (ex

C:\>Echo Loop Variable is =="6f0c96.msi"==
Loop Variable is =="6f0c96.msi"==

C:\>Echo Loop Variable is =="6f0c97.mst"==
Loop Variable is =="6f0c97.mst"==

C:\>Echo Loop Variable is =="96500b.mst"==
Loop Variable is =="96500b.mst"==

C:\>Echo Loop Variable is =="aruser.chm"==
Loop Variable is =="aruser.chm"==

Using this:

@Echo Off
SetLocal
Set _Path=C:\TEMP

For /F "tokens=1 delims=" %%I In ('ForFiles /P "%_Path%" /D -45') Do Set _Result=%%I
If /I "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles

ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File" 2>Nul
Echo Files were deleted
Goto :EOF

:_NoFiles
Echo No files were found to delete


Gets me a listing of all the files in the folder but not the actual folders that I need.


----------



## Nnyan (Aug 7, 2000)

I started playing around with your original first suggestion and when I changed the following line from Errorlevel 1 to 0:

If %Errorlevel%==0 Goto _NoFiles

It works and does what I need it to (doesn't check first but I can live with that).


----------



## TheOutcaste (Aug 8, 2007)

Remember to change the *@ISDIR==FALSE* to *@ISDIR==TRUE* when looking for folders


----------



## Nnyan (Aug 7, 2000)

OK 

Fixed my screw-ups this works (no checks but again thats fine):

@Echo Off
SetLocal
Set _Path=C:\TEST

REM ForFiles /P "%_Path%" /D -4 /C "%ComSpec% /C If @ISDIR==TRUE Echo RD /Q /S @File"
ForFiles /P "%_Path%" /D -2 /C "CMD /C if @ISDIR==TRUE echo RD /Q /S @FILE" 2>Nul

If %Errorlevel%==1 Goto _NoFiles
Echo Files were deleted
Goto :EOF

:_NoFiles
Echo No files were found to delete


----------



## Nnyan (Aug 7, 2000)

Anyone know how I can do a check to see if there are files that meet the requirements before I do the delete?


----------



## Nnyan (Aug 7, 2000)

Playing around with these two lines:

For /F "tokens=1 delims=" %%I In ('ForFiles /P "D:\FS-BACKUP\Purged" /D -30 2^>^&1') Do Set _Result=%%I
If "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles

Gets me a check that seems to work BUT I get a listing for each file it finds. Anyway to stop that from listing?


----------



## Squashman (Apr 4, 2003)

You redirecting the output to standard output. Since standard output is not redirecting to a file it will redirect to the console.


----------



## TheOutcaste (Aug 8, 2007)

Those two lines will not echo anything to the screen. I've redirected STDERR (handle 2) to STDOUT (handle 1) so that any error will be set to the loop variable and end up in *_Result* rather than just be displayed on screen. This way we can check if files meeting the criteria were found.

If you are seeing the files on the screen, it's some other line, like this one:


```
ForFiles /P "%_Path%" /D -7 /C "%ComSpec% /C If @ISDIR==FALSE Echo Del @File & Del @File" 2>Nul
```
This is line 6 from the 2nd section of code in Post#5
The Echo statements in that line will Echo the output.

The only thing this will display is whether or not there are files to process. I pass the # of Days as a command line argument to make testing easier:

```
@Echo Off
SetLocal
Set _Days=%1
For /F "tokens=1 delims=" %%I In ('ForFiles /P "C:\tstfldr1" /D %_Days% 2^>^&1') Do Set _Result=%%I
If "%_Result%"=="ERROR: No files found with the specified search criteria." Goto _NoFiles
Echo There are files to process
Goto :EOF
:_NoFiles
Echo No files were found to Process
```
Here's the result:

```
C:\tstfldr1>check.cmd -1
There are files to process

C:\tstfldr1>check.cmd -100
No files were found to Process
```
Without seeing the complete code there's no way to tell what is generating the output.


----------

