nav2567
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.
Please advise.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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:
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.
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.
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.
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")
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 . . .
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks everyone. JPRobert's script works best in our environment. Thanks again.
Open in new window