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!
LVL 2
Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
That's just an unwanted effect when it doesn't find qualified files. This is fixable via the second solution, i.e. "This should solve that issue". See https://stackoverflow.com/questions/16820681/suppress-forfiles-no-files-found-error
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yo_beeDirector of Information TechnologyCommented:
are you opposed to Powerhsell? If not I can post a script?
0
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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?
0
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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.
0
yo_beeDirector of Information TechnologyCommented:
Yes it can me automated.
Do you want the parent directory deleted as well?
0
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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.
0
yo_beeDirector of Information TechnologyCommented:
Here you go.
Copy and paste into notepad.
Save as PS1 file type
to run this you will schedule a task to  run powershell.exe and the argument will be the full path to the script.
2018-02-01_15-57-06.pngIf 4 days is to little you can change -4 to whatever number of days you wish.
$dateDiff = (get-date).adddays(-4) 
get-childitem -path 'C:\Users\xxxxxxx\Downloads' -directory |?{$_.LastWriteTime -lt $dateDiff} | Remove-Item

Open in new window

0
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
thanks!
0
yo_beeDirector of Information TechnologyCommented:
Happy to help.  If you have any questions please ask.
0
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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.
0
NVITEnd-user supportCommented:

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.
0
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
thanks nvit. I'll write up a new ticket if the ps1 doesn't work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.