Test-Path incorrect file validation

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

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

 
Participant
1 week, 2 days ago.

  • Author
    Posts
  • #132927

    Participant
    Points: 168
    Helping Hand
    Rank: Participant

    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

    Participant
    Points: 1,086
    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: 398
    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. 😉

  • #132953

    Participant
    Points: 168
    Helping Hand
    Rank: Participant

    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: 168
    Helping Hand
    Rank: Participant

    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: 398
    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. 😉

  • #132963

    Participant
    Points: 168
    Helping Hand
    Rank: Participant

    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: 384
    Helping Hand
    Rank: 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: 168
    Helping Hand
    Rank: Participant

    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...

You must be logged in to reply to this topic.