Test-Path incorrect file validation

Welcome Forums General PowerShell Q&A Test-Path incorrect file validation

This topic contains 10 replies, has 4 voices, and was last updated by

 
Participant
2 months, 2 weeks ago.

  • Author
    Posts
  • #132927

    Participant
    Points: 343
    Helping Hand
    Rank: Contributor

    This is an expected behavior:

    PS C:\> Test-Path -Path C:\MyFolder\ -IsValid -PathType Container
    True
    PS C:\>

    However, this seems to be incorrect:

    PS C:\> Test-Path -Path C:\MyFolder\ -IsValid -PathType Leaf
    True
    PS C:\>

    According to the official documentation:

    "Leaf. An element that does not contain other elements, such as a file."

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-6

    Is it a bug or did I miss something?

  • #132932

    Moderator
    Points: 1,991
    Helping Hand
    Rank: Community Hero

    what version of PowerShell you are using ? I cannot repro this in PSCore 6.2.0-Preview.3

  • #132935

    Participant
    Points: 1,375
    Helping Hand
    Rank: Community Hero

    You should read the documentation more carefully:

    -IsValid

    Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist.

    If you ommit the parameter -IsValid it should work as you probably expect it to work. 😉

  • #132953

    Participant
    Points: 343
    Helping Hand
    Rank: Contributor

    what version of PowerShell you are using ? I cannot repro this in PSCore 6.2.0-Preview.3

    I tried with both PS 5.1 and 6.1

  • #132956

    Participant
    Points: 343
    Helping Hand
    Rank: Contributor

    You should read the documentation more carefully:

    -IsValid

    Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist.

    If you ommit the parameter -IsValid it should work as you probably expect it to work.

    That's exactly what I want to do: validating the syntax regardless whether the object already exists or not.

    I am using the IsValid parameter on purpose in order to check the syntax before trying to create a file.

    This means for me that IsValid is not able to detect that C:\MyFolder\ is always a folder object and cannot be a leaf.

  • #132959

    Participant
    Points: 1,375
    Helping Hand
    Rank: Community Hero

    It seems to be like lit on StackOverflow already said – you cannot detect if it's a container or a file if it's not existing yet. 😉

  • #132963

    Participant
    Points: 343
    Helping Hand
    Rank: Contributor

    It seems to be like lit on StackOverflow already said – you cannot detect if it's a container or a file if it's not existing yet.

    Ok then if IsValid is not able to make the difference between a leaf and a container, then it doesn't make sense to have both IsValid and PathType in the same parameter set and they should be mutually exclusive.

    I am considering to open an issue on Github.

  • #132996

    Participant
    Points: 661
    Helping Hand
    Rank: Major Contributor

    Might be a good idea, I suppose. But yeah, since a file can in principle simply not have any extension and still be a file, it's impossible for the cmdlet to determine whether a given arbitrary path would be an invalid leaf.

    Mind you, in Windows file systems \ isn't a valid filename character so it could try to use that, but then you run into issues with other file systems using different path-separators. Perhaps in combination with this PR it could use that to determine, however?

  • #133065

    Participant
    Points: 343
    Helping Hand
    Rank: Contributor

    Mind you, in Windows file systems \ isn't a valid filename character so it could try to use that, but then you run into issues with other file systems using different path-separators. Perhaps in combination with this PR it could use that to determine, however?

    I don't know if  a final slash is making a difference between files and folders on a Linux system. Would be interesting to know...

  • #138054

    Participant
    Points: 343
    Helping Hand
    Rank: Contributor

    Thanks everybody for helping me to figure this out.

    Here is the final outcome:

    According to Microsoft in Github issue #8607:

    -IsValid and -PathType should have been in separate parameter sets

    This has been referenced for a breaking change and the -PathType parameter has been appended with a Caution paragraph in the official documentation.

  • #138083

    Participant
    Points: 1,375
    Helping Hand
    Rank: Community Hero

    Cool. Well done you. Thanks a lot.

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort