New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improving Test-Path for when conditionally creating a new path #18957
Comments
Perhaps a more elegant solution would be to add a switch to New-Item (and New-ItemProperty): |
|
I just use |
A switch such as The problem with registry keys losing their properties (values) with The backward-compatible way to resolve this issues is to introduce a |
I like the This sort of syntax might be useful if this were possible: |
It's an interesting idea, and while I like the DRY aspect of it, the fact that it would only work with commands that expect exactly one argument (namely the path being tested) makes this too narrow a feature in my view. A more flexible alternative could be to add a # WISHFUL THINKING
Test-Path -PassThru 'L:\some\long\path\' | % { "$_ exists." }
Test-Path -Not -PassThru 'L:\some\long\path\' | % { "$_ does NOT exist." } That is, |
@Andrew74L, re-reading your initial post I see that you had already proposed similar ideas. Tor recap re A debate about whether |
Thanks MK. In considering this I'm trying to dream up a syntax that has more of a natural language feel to it. Will |
Perhaps Resolve-Path is the better candidate for this concept, as it already returns paths if exist. The idea would be to execute a scriptblock if path not found, but is valid.
|
Honestly, for the use case at hand, It has the additional advantage that, should an error then occur, it signals a true error condition. |
Okay. I'm curious about what the process would be for getting |
I have very little insight into what ultimately guides the decisions - I'm just a fellow user - but, generally speaking:
Publicly signaling interest in a feature is an important part of the process, so I suggest you give a 👍 to those feature requests you would personally like to see implemented. |
Thanks for that info. Regarding experimental features discovery, for preview versions I would make the logo something like:
I'll keep that in mind. |
Re experimental-feature discovery: sounds like a good suggestion, I encourage you to post it at #14862 |
Similar thoughts (wishful thinking): $Profile.PSObject.Properties | Where-Object value -Like *profile.ps1 |
Test-Item -PassThru | Rename-Item $_.Value "$($_.Value).Bak" -Force |
Hey, that's a pretty good idea. Works out alright and while a New-Item foo1, foo2
"foo1", "foo2", "foo3" | Resolve-Path -ErrorAction Ignore | Remove-Item Doing the reverse is a bit more of a pain "foo1", "foo2", "foo3" |
Where-Object { -not (Resolve-Path $_ -ErrorAction Ignore) } |
New-Item -Path { $_ } Now that I think about it you can also do "foo1", "foo2", "foo3" | ? { Test-Path $_ } | Remove-Item -Path { $_ } |
It would be nice if this functionality was native but in the meantime, this might be helpful. You may want to verify the logic before using it if you're deleting files though. Edit: Changed "Where-TestPath" to "Filter-OnTestPath" because "Where" is a reserved verb. "Filter" is unapproved but what's the approved alternative to "Where" anyway? function Filter-OnTestPath {
[CmdletBinding()]
param(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string[]] $Path,
[ValidateSet('Existing', 'NonExisting')]
[string] $Status = 'Existing',
[switch] $Name
)
process {
foreach ($Item in $Path) {
$FileExists = Test-Path $Item
$DoesExist = $Status -eq 'Existing' -and $FileExists
$IsAbsent = $Status -eq 'NonExisting' -and -not $FileExists
if ($DoesExist -or $IsAbsent) {
if ($Name) { $Item }
else { [PSCustomObject] @{ Path = $Item } }
}
}
}
}
@('foo', 'foo2', 'foo3') | Filter-OnTestPath -Status NonExisting | New-Item -Verbose | Out-Null
@('foo', 'foo2', 'foo3') | Filter-OnTestPath -Status Existing | Remove-Item -Verbose |
Summary of the new feature / enhancement
Test-Path script often looks like this:
if (-not (Test-Path 'L:\some\long\path\')) {New-Item 'L:\some\long\path\'}
Having to specify the path twice, is not very efficient.
This is a small improvement:
$p = 'L:\some\long\path\'
if (-not (Test-Path $p)) {New-Item $p}
I'm wondering if Test-Path could be improved in a way that streamlines code when conditionally creating a new path.
Proposed technical implementation details (optional)
Suppose Test-Path could pass a PSProvider object down the pipeline, based on the -Path argument. Hypothetically:
Test-Path 'L:\some\long\path\' -PassThru || New-Item
This wouldn't work because the command hasn't failed. However, suppose the result of Test-Path could be easily negated with a False switch, like this:
Test-Path 'L:\some\long\path\' -False -PassThru && New-Item
This still isn't going to work because the object would be passed-through regardless of the $true/$false outcome. So what about this:
Test-Path 'L:\some\long\path\' -PassThruIfFalseElseFail && New-Item
I'm not sure this would be particularly "intuitive", so what would be a better way, or is it a non-issue?
The text was updated successfully, but these errors were encountered: