Need check if file exist older than n days then do a delete.
Posted: 09 May 2011 10:00
I'm not familiar in writing .bat scripting at all. I'm trying to do the following and need help!
I know there is a command ForFiles that you can use to delete file older than n days. BUT I want to be able to check to make sure there are files to delete first (like do a file count) before actually using the ForFiles command. I am having a job scheduler (ActiveBatch) to execute this batch file. What I find out is that if I only used the one line ForFiles to do the delete, it will fail my job because if there isn't a file to delete, the batch file reports a failure to the job scheduler. That is what I'm trying to avoid.
I would like:
1. Find files with a specific extension in a directory that is older than n days, if exists.
2. If so, use the ForFiles command to delete the files that is older than n days.
3. If not, then don't do anything.
pseudo:
if exists (count of files older than n days > 1 ) then
forfiles ---delete files older than n days
else -- if there are no files that fits that criteria then
exit script.
Please help me script this out. Your help is greatly appreciated!
Thank you in advance.
I know there is a command ForFiles that you can use to delete file older than n days. BUT I want to be able to check to make sure there are files to delete first (like do a file count) before actually using the ForFiles command. I am having a job scheduler (ActiveBatch) to execute this batch file. What I find out is that if I only used the one line ForFiles to do the delete, it will fail my job because if there isn't a file to delete, the batch file reports a failure to the job scheduler. That is what I'm trying to avoid.
I would like:
1. Find files with a specific extension in a directory that is older than n days, if exists.
2. If so, use the ForFiles command to delete the files that is older than n days.
3. If not, then don't do anything.
pseudo:
if exists (count of files older than n days > 1 ) then
forfiles ---delete files older than n days
else -- if there are no files that fits that criteria then
exit script.
Please help me script this out. Your help is greatly appreciated!
Thank you in advance.