Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

remove contents of a file path older than 30 days.

We need help to put together a batch script to navigate to a folder c:\abc\logs and remove any contents (directories or files) with modified date older than 30 days.  

Please advise.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of John Gobert
John Gobert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The following should do the trick... just adjust the maxage variable and pathtoclean to match the path you want.

@echo off
:: set cleanup path
set pathtoclean=c:\abc\logs
 
:: set the max age in days
set maxage=30
 
:: remove files from %pathtoclean%
forfiles -p %pathtoclean% -m *.* -d -%maxage% -c "cmd  /c del /q @path"
 
:: clean up subfolders %pathtoclean%
forfiles -p %pathtoclean% -d -%maxage% -c "cmd /c IF @isdir == TRUE rd /S /Q @path"

Open in new window

SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nav2567

ASKER

jbgobert, would you modify your script to delete folders starting with 2 only?

There are a lot of subfolders we want to remove start with 20130101, 20130102......

Thanks everyone.
Are these subfolders under the path C:\abc\logs ?

Examples:  
c:\abc\logs\20130101
c:\abc\logs\20130102

If so, you still want to delete the contents and the folder itself, correct?  Are there any additional subfolders under the date-named folders?

Let me know and I'll post an update shortly.
Avatar of nav2567

ASKER

That's correct.  The c:\abc\logs folder has a lot of subfolders starting with 2013........

The thing is there are other system folders in c:\abc\logs which we do not want to touch and exclude those.

Thanks.
Here's the update.  If you look at line 9 you'll see %pathtoclean%2* in the FOR loop's filter.  The 2* causes only folders starting with 2 should be included.  If you wanted to expand that to be 2013 then it would be %pathtoclean%2013*.  Make sense?

@echo off

:: set cleanup path
set pathtoclean=C:\abc\logs\
 
:: set the max age in days
set maxage=30

FOR /F %%i in ('DIR /b /ad %pathtoclean%2*') DO SET FOLDER_NAME=%%i & CALL :SUB_CLEANUP %FOLDER_NAME%

:SUB_CLEANUP

SET FULL_PATH = %pathtoclean%%FOLDER_NAME%
FORFILES /P %FULL_PATH% /M *.* /D -%maxage% /C "CMD /C ECHO Y | DEL @PATH")
FORFILES /P %FULL_PATH% /D -%maxage% /C "CMD /C IF @isdir == TRUE RMDIR /S /Q @PATH")

Open in new window

Avatar of nav2567

ASKER

No good.  I am seeing this when I run:

File Not Found
ERROR: Invalid syntax. Value expected for '/P'.
Type "FORFILES /?" for usage.
ERROR: Invalid syntax. Value expected for '/P'.
Type "FORFILES /?" for usage.
Press any key to continue . . .
What's the OS you're running this on?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nav2567

ASKER

Thanks everyone.  JPRobert's script works best in our environment.  Thanks again.