How can I get a nice list of files in a directory tree that contains multiple OLD Files,
I d'like to see only files from directories named OLD that have a certain age.
How can I get a nice list of files in a directory tree that contains multiple OLD Files,
I d'like to see only files from directories named OLD that have a certain age.
Well first part list all the files, second part filter your files, and finally format your output. You can use format-list or table (but I don't have a powershell nearby to test it :])
$yourDate = Get-Date 5/1/2006
ls -recurse . | ? { $_.fullname -match "OLD" -and $_.lastwritetime -lt $yourDate } | % { $_.fullname }
Get-Date create a Date-Time object when you give him a specific date as parameter, just use it filter then.
As I understand the question, the solution above doesn't quite answer it. Instead of finding all files from directories that are named "OLD" the solution above finds all files that contain "OLD" in their name.
Instead, I think you're asking for something that finds all files older than a certain date that are in directories named OLD.
So, to find the directories, we need something like the following:
dir -r | ? {$_.name -match "\bold\b" -and $_.PSIsContainer}
But you then need something that can recursively go through each directory and find the files (and, potentially, 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's a way to do it on one line that takes a different tactic (note the line continuation character so this would fit:
dir -r | ? {!$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date 5/1/2006)} `
| ? {(% {$_.directoryname} | split-path -leaf) -eq "OLD"}
So, what's happening here?
The first section is just a basic recursive directory listing. The next section checks to be sure that you're looking at a file (!$_.PSIsContainer
) and that it meets your age requirements. The parentheses around the Get-Date
section let you get the results of running the command. Then we get the directory name of each file and use the split-path commandlet to get just the name of the closest directory. If that is "OLD" then we have a file that matches your requirements.