Reputation: 30524
I'm writing a PowerShell script in which I need to get a list of files that have complementary .md5 files. For example, if I have a file name abc.txt
I only want to add it to the list if there exists a file named abc.txt.md5
in the same directory.
This is my attempt, but it's not working. I'm not sure why?
$DirectoryToScan = ".\SomePath"
$Files = Get-ChildItem $DirectoryToScan -Recurse |
Where-Object { !$_.PSIsContainer } |
Where-Object { $_.Name -notmatch ".*\.md5" } |
Where-Object { Test-Path "$($_.FullName).md5" }
It works fine without the last Where-Object
clause.
Upvotes: 2
Views: 342
Reputation: 126842
Get-ChildItem $DirectoryToScan -Recurse | `
Where-Object { !$_.PSIsContainer -and (Test-Path "$($_.FullName).md5" -PathType Leaf)}
Upvotes: 1
Reputation: 68303
I think this should work, and save you hitting the directory again for the test-path.
$DirectoryToScan = ".\SomePath"
$temp = Get-Childitem $DirectoryToScan -recurse | select -expand fullname
$files = $temp |
foreach {if ($_ -notmatch '\.md5$' -and $temp -contains "$_.md5"){$_}}
Upvotes: 1
Reputation: 301257
What you had give does work for me, but you can try doing something like this:
gci $DirectoryToScan -recurse -exclude "*.md5" | ?{ -not $_.PsIsContainer } |
?{ test-path ($_.fullname + ".md5") }
Upvotes: 2