开发者

Find files only in subdirectories named OLD with a certain age using PowerShell

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.


As I understand the question, Raoul Supercopter's solution 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 cmdlet to get just the name of the closest directory. If that is "OLD" then we have a file that matches your requirements.


Well, the first part is to list all the files, the second part filter your files, and finally format your output. You can use Format-List or table (but I don't have a PowerShell installation nearby 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜