Add FileSystem provider to list of supported filters for Test-Path. It must behave like Get-ChildItem -Filter
Set-Content -Path 'C:\Users\username\Documents\test.txt' -Value 'Hello, World!'
Test-Path -Path 'C:\Users\username\Documents\test.txt' -Filter '*.json'
False
True
Do any currently implemented providers provide a -Filter for Test-Path? 馃 I'm not aware of there being any.
@vexx32 don't think so but to be fair, no other built-in provider has support for Filter in any capacity (at least of those I've tested).
This wouldn't be super easy to add though I don't think. Currently it's just a property on CmdletProvider called Filter. The provider registers that it supports filters by declaring it as part of their ProviderCapabilities. It's sort of all or nothing, so if this were added to Test-Path you'd need a new ProviderCapabilities value like FilterItemExists. And/or maybe a new method to test ItemExists with a filter, plus new interface and/or base class to implement/inherit from.
Yeah, that's my thought tbh, the file system provider is the only one I've ever seen a filter on, so I was curious if something else had tried to put filters on Test-Path.
It's an unusual use case too; Test-Path is usually used for testing complete paths, whereas -Filter is generally used to qualify searches (Get-ChildItem/Get-Item) and to filter out certain results. I kind of get the intent here, but I'm not really sure what providing -Filter would add here; you already have the path itself, so you can test if the string ends with a specific extension with more direct methods, or use Split-Path to retrieve just the filename if you have more complex patterns to test against. 馃槙
Do any currently implemented providers provide a
-FilterforTest-Path? 馃 I'm not aware of there being any.
@vexx32, no. Not yet. It's not mentioned in docs at least
Yeah, that's my thought tbh, the file system provider is the only one I've ever seen a filter on, so I was curious if something else had tried to put filters on Test-Path.
It's an unusual use case too; Test-Path is usually used for testing complete paths, whereas -Filter is generally used to qualify searches (Get-ChildItem/Get-Item) and to filter out certain results. I kind of get the intent here, but I'm not really sure what providing -Filter would add here; you already have the path itself, so you can test if the string ends with a specific extension with more direct methods, or use Split-Path to retrieve just the filename if you have more complex patterns to test against. 馃槙
@vexx32, Get-ChildItem supports two way for filtering:
-Filter
-Include/-Exclude
The point is that flag -Filter can filter results outside PowerShell. -Include/-Exclude filter results inside PowerShell.
It's mentioned in Get-ChildItem docs:
Filters are more efficient than other parameters. The provider applies filter when the cmdlet gets the objects rather than having PowerShell filter the objects after they're retrieved
It must behave like Get-ChildItem -Filter
I wonder if it works:
dir C:\tmp\test1.txt -Filter "*.json"
Directory: C:\tmp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 28.04.2020 21:26 14 test1.txt
It must behave like Get-ChildItem -Filter
I wonder if it works:
dir C:\tmp\test1.txt -Filter "*.json" Directory: C:\tmp Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 28.04.2020 21:26 14 test1.txt
@iSazonov, It's another one bug definitely. Try:
dir C:\tmp\ -Filter '*.json'
It will return only .json files. I have tested it.
It work in more predictable way in plain old CMD:
dir \temp\
10.10.2020 21:03 <DIR> .
10.10.2020 21:03 <DIR> ..
10.10.2020 21:02 0 test.json
10.10.2020 21:02 0 test.txt
dir "\temp\*.json"
10.10.2020 21:02 0 test.json
dir \temp\test.txt
10.10.2020 21:02 0 test.txt
dir "\temp\test.txt\*.json"
肖邪泄谢 薪械 薪邪泄写械薪 (No such file)
Yeah I think fundamentally the way the Filter functionality is designed, it tends not to apply when you supply a completely unambiguous single file path; there's nothing for it to filter in the first place, there's only one thing it can be.
Whether that should change now is an open question, I guess.
Most helpful comment
Yeah, that's my thought tbh, the file system provider is the only one I've ever seen a filter on, so I was curious if something else had tried to put filters on Test-Path.
It's an unusual use case too; Test-Path is usually used for testing complete paths, whereas -Filter is generally used to qualify searches (Get-ChildItem/Get-Item) and to filter out certain results. I kind of get the intent here, but I'm not really sure what providing -Filter would add here; you already have the path itself, so you can test if the string ends with a specific extension with more direct methods, or use Split-Path to retrieve just the filename if you have more complex patterns to test against. 馃槙