• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 67
  • Last Modified:

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!
0
Starr Duskk
Asked:
Starr Duskk
  • 7
  • 4
  • 2
2 Solutions
 
NVITCommented:
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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
NVITCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now