Find files only in subdirectories named OLD with a specific age using PowerShell
As I understand it, Raoul Supercopter's Solution doesn't quite answer it. Instead of finding all files from directories named OLD, the solution above finds all files that contain "OLD" in their name.
Instead, I think you are asking for something that finds all files older than a certain date that are in directories named OLD.
So, to find directories, we need something like the following:
dir -r | ? {$_.name -match "\bold\b" -and $_.PSIsContainer}
But you then need something that can recursively walk through each directory and find files (and possibly any directories named "OLD" that are contained in other directories named "OLD").
The easiest way to do this is to write a function and then call it recursively, but here you can do it on one line, which takes a different tactic (note the line-continuation character to match:
dir -r | ? {!$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date 5/1/2006)} `
| ? {(% {$_.directoryname} | split-path -leaf) -eq "OLD"}
So what's going on here?
The first section is just a recursive directory listing. The next section verifies that you are looking at the file ( !$_.PSIsContainer
) and that it is age-appropriate. The parentheses in the Get-Date section allow you to get the results of the command. We then get the directory name of each file and use the split-path cmdlet to get only the name of the nearest directory. If it is "OLD" then we have a file that suits your needs.
a source to share
Well the first part is a list of all files, the second part filters your files and finally formats your output. You can use Format-List or Table (but I don't have a PowerShell setup to test it:])
$yourDate = Get-Date 5/1/2006
ls -recurse . | ? { $_.fullname -match "OLD" -and $_.lastwritetime -lt $yourDate } | % { $_.fullname }
Get-Date creates a Date-Time object when you give it a specific date as a parameter. Just use it and filter.
a source to share