Tim Barrass
Tim Barrass

Reputation: 4939

Delete files in directory without having to wait for whole list

I have a large number F of files to sort through (5M+) and want to delete everything older than N days.

Is there any way of starting to delete files before the whole set has been identified -- perhaps equivalent to

System.IO.DirectoryInfo($folder).EnumerateFiles( ... ) 

in C#?

Upvotes: 0

Views: 250

Answers (1)

JonC
JonC

Reputation: 978

The Get-ChildItem already does this. Files are passed down the pipes as soon as they are processed. Try this;

$NDays = -5
dir *.* | where{$_.CreationTime -lt (Get-Date).AddDays($NDays)} | Remove-Item

Upvotes: 2

Related Questions