Skip to content

Split-Path switches (-Leaf, -Parent, ...) do not work with -LiteralPath #8751

Closed

Description

Note: This affects all switches supported by Split-Path; combining them with the (implied) -Path works fine, but with -LiteralPath the parameter set cannot be resolved.

Steps to reproduce

Split-Path -LiteralPath foo/bar
Split-Path -Parent -LiteralPath foo/bar
Split-Path -Leaf -LiteralPath foo/bar

Expected behavior

foo
foo
bar

Actual behavior

foo
Parameter set cannot be resolved using the specified named parameters. ...
Parameter set cannot be resolved using the specified named parameters. ...

Environment data

PowerShell Core v6.2.0-preview.3 on macOS 10.14.2
PowerShell Core v6.2.0-preview.3 on Ubuntu 18.04.1 LTS
PowerShell Core v6.2.0-preview.3 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.471)
Windows PowerShell v5.1.17134.407 on Microsoft Windows 10 Pro (64-bit; Version 1803, OS Build: 17134.471)

Activity

  1. changed the title [-][BUG] Split-Path switches (-Leaf, -Parent, ...) do not work with -LiteralPath[/-] [+]Split-Path switches (-Leaf, -Parent, ...) do not work with -LiteralPath[/+] on Feb 2, 2019
  2. jagadeesh408 commented on Jun 1, 2020

    @jagadeesh408

    I am also facing the same issue. Does this issues resolved?
    If yes, can someone guide me on the solution

  3. mklement0 commented on Jun 1, 2020

    @mklement0
    ContributorAuthor

    It looks like the workaround is to simply use -Path, which - unlike with other cmdlets - doesn't actually treat its argument as a wildcard pattern in the case of Split-Path; in other words: -Path apparently acts the way that -LiteralPath normally does.

  4. jagadeesh408 commented on Jun 1, 2020

    @jagadeesh408

    but i have a path like date\matter\movva, jagadeesh [jagadeesh408] and if i am using -path to get the leaf it is skipping the result due to the special characters [].
    When i am using -literalpath for removing the leaf from the path it is working fine but not for getting the leaf

  5. mklement0 commented on Jun 1, 2020

    @mklement0
    ContributorAuthor

    I don't see this behavior (neither in WinPS 5.1, nor in 7.0, nor in 7.1.0-preview.3); do you get a different result, or am I missing something?

    PS> Split-Path -Path 'date\matter\movva, jagadeesh [jagadeesh408]' -Leaf
    movva, jagadeesh [jagadeesh408]
  6. jagadeesh408 commented on Jun 1, 2020

    @jagadeesh408

    I am using this way
    split-Path -path "'$($filepaths1[$i])'" -Leaf -Resolve
    where $filepaths1 is array of different paths and getting the error as below

    split-Path : Cannot find drive. A drive with the name ''D' does not exist.

    when i am using the command only with single quotes as below, it is not taking the variable also as string

    split-Path -path '$($filepaths1[$i])' -Leaf -Resolve

  7. mklement0 commented on Jun 1, 2020

    @mklement0
    ContributorAuthor

    Do not enclose the file path in embedded ' characters - such a path won't be valid; also don't use ' as the syntactic quotes, for the reasons you state (single-quoted strings are verbatim strings).

    Just use the expression as-is, without quoting:

    split-Path -path $filepaths1[$i] -Leaf -Resolve

    If you want to use -Resolve, you actually must use -Path, not -LiteralPath (which is strange in itself).

  8. jagadeesh408 commented on Jun 1, 2020

    @jagadeesh408

    if i am using this
    split-Path -path $filepaths1[$i] -Leaf -Resolve

    whenever $filepaths1 have a path like date\matter\movva, jagadeesh [jagadeesh408] with special characters like [], it is skipping the result and providing me the null results

  9. mklement0 commented on Jun 1, 2020

    @mklement0
    ContributorAuthor

    (Correction to my earlier comment, which I removed.)

    Since you're only looking for the leaf (file name) component, simply omit the -Resolve part, which prevents the unwanted interpretation of the input paths as wildcard expressions:

    split-Path -path $filepaths1[$i] -Leaf

    That said, it does make sense to combine -Resolve with -LiteralPath as well, namely to expand the - literal, but possibly relative - path to a full path before extracting the part of interest.

    For instance, you may want to use -Qualifier to extract the drive spec. from a literal path specified as a relative path (without -Resolve, the relative path would be interpreted as-is, and therefore has no drive spec.); this is currently impossible:

    # !! Currently fails, due to the bug at hand.
    Split-Path -LiteralPath file.txt -Resolve -Qualifier
  10. jagadeesh408 commented on Jun 1, 2020

    @jagadeesh408

    Thanks,
    I will test it and update the thread if any issues

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions