Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

Removing old files and directories using batch files.

I'd like to  remove files and subdirectories that go 4 levels down after X days.

If the parent directory is X days old, then remove the subdirs. But if the parent directory is not X days old, don't taverse the files inside, which can have older files in that directory.

I have tried this which I found on another site. But it errors;
Error: No files found with the specified search criteria.


:: set folder path
set dump_path=D:\BUPS\ThirtyMinAll

:: set min age of files and folders to delete
set max_days=3

:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd  /c del /q @path"

:: remove sub directories from %dump_path%
forfiles -p %dump_path% -d -%max_days% -c "cmd /c IF @isdir == TRUE rd /S /Q @path"

How can I do this please?

thanks!
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
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
are you opposed to Powerhsell? If not I can post a script?
Avatar of Starr Duskk

ASKER

Yo_bee I do have powershell but have never used it. Can it be automated on a schedule and you can show me how? if so, yes, that would be fine.
NVIT, which one of those solutions specifically? Who is the author? I know you said second. You mean the one by malcolm, with 4 votes?
NVIT, I did that one and it seemed to work. Going to give it a thorough check it make sure it didn't delete older files in the subdirs that I wanted to keep. But it's rebooting now.
Yes it can me automated.
Do you want the parent directory deleted as well?
yes, I do yo_bee.

It appears that NVITs answer does work though. I haven't had a chance to thoroughly check all directoris that remain to make sure my files are in tact, but I think they are.

But I am interested in learning what this powershell thing can do.

And remember, I don't want to directory containers deleted they are older than the directory itself.
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
thanks!
Happy to help.  If you have any questions please ask.
I just might. I'm finding the other method, you have to have the .bat file in the directory, but when run on a schedule, it deletes itself.

So I'll try your method. It doesn't delete itself does it? And do I have to store the ps1 file in the same directory I'm trying to delete?

I'm going to try it this weekend.

you have to have the .bat file in the directory, but when run on a schedule, it deletes itself

Probably just need to adjust your code a bit. So you don't have to put it in the same folder.

If you like, post your code. I'm sure someone can help you.
thanks nvit. I'll write up a new ticket if the ps1 doesn't work.