none
Powershell resolve path parameter when path does not exist RRS feed

  • Question

  • Hello,

    I'd like your help with the following issue I'm facing.

    I have a function that takes the path to a file as a parameter but this file does not exist, the function will create it.

    Let's say for example that the function creates a report and it's called like New-Report -Path file.txt.

    How can I get the full name of the file? I can be in many formats like file.txt, .\file.txt, c:\file.txt but I need the full path.

    Regards,

    Chris

    Friday, July 1, 2016 5:33 PM

Answers

  • Thank you for your suggestions, I managed to get what I'm looking for with the following function:

    Function Resolve-NonExistentPath
    {
        <#
        .SYNOPSIS
            Get the full path using a relative path of a file or folder that does not exist.
    
        .DESCRIPTION
            The Resolve-NonExistentPath cmdlet will return the full path to a file or folder that does not exist.
    
        .PARAMETER Path
            The path to resolve.
    
        .EXAMPLE
            Resolve-NonExistentPath -Path test.txt
    
            This will return the full path to the test.txt path
        #>
    
        Param
        (
            [string]$Path
        )
    
        $fullpath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
    
        $obj = New-Object psobject
        $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $Path
        $obj | Add-Member -MemberType NoteProperty -Name "FullName" -Value $fullpath
    
        $obj
    }

    Friday, July 1, 2016 9:04 PM

All replies

  • Help resolve-Path

    \_(ツ)_/

    • Proposed as answer by jrv Friday, July 1, 2016 9:12 PM
    • Unproposed as answer by jrv Friday, July 1, 2016 9:12 PM
    • Proposed as answer by jrv Friday, July 1, 2016 9:12 PM
    • Unproposed as answer by jrv Friday, July 1, 2016 9:12 PM
    • Proposed as answer by jrv Friday, July 1, 2016 9:12 PM
    Friday, July 1, 2016 5:46 PM
  • I'm confused as to what you're really asking, but you can use the FullName property to get the full path of a file.

    PS C:\Scripts\PowerShell Scripts\Misc Testing\7-01-2016> (Get-Item .\file.txt).FullName
    C:\Scripts\PowerShell Scripts\Misc Testing\7-01-2016\file.txt


    Friday, July 1, 2016 5:47 PM
  • I can be in many formats like file.txt, .\file.txt, c:\file.txt but I need the full path.

    You don't need the full path if the file doesn't exist, but you can get it after you created it. Just get the FullName property of the file.


    -- Bill Stewart [Bill_Stewart]

    Friday, July 1, 2016 5:51 PM
    Moderator
  • Thank you for your suggestions, I managed to get what I'm looking for with the following function:

    Function Resolve-NonExistentPath
    {
        <#
        .SYNOPSIS
            Get the full path using a relative path of a file or folder that does not exist.
    
        .DESCRIPTION
            The Resolve-NonExistentPath cmdlet will return the full path to a file or folder that does not exist.
    
        .PARAMETER Path
            The path to resolve.
    
        .EXAMPLE
            Resolve-NonExistentPath -Path test.txt
    
            This will return the full path to the test.txt path
        #>
    
        Param
        (
            [string]$Path
        )
    
        $fullpath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
    
        $obj = New-Object psobject
        $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $Path
        $obj | Add-Member -MemberType NoteProperty -Name "FullName" -Value $fullpath
    
        $obj
    }

    Friday, July 1, 2016 9:04 PM
  • Thank you for your suggestions, I managed to get what I'm looking for with the following function:

    Function Resolve-NonExistentPath
    {
        <#

    Which is exactly what this is: Resolve-Path test.ps1

    And it is built into PowerShell.

    Always learn the basics first.  Why waste time on guessing when it will only teach you bad habits and poor solutions.

    get-Command -Noun *path*


    \_(ツ)_/


    • Edited by jrv Friday, July 1, 2016 9:11 PM
    • Proposed as answer by jrv Friday, July 1, 2016 9:11 PM
    Friday, July 1, 2016 9:09 PM
  • Hi jrv,

    You are missing the fact that the file does not exist. Resolve-Path only works on existing files and directories. 

    Chris


    Friday, July 1, 2016 9:44 PM
  • I don't know why you need the path to a file that doesn't exist. It doesn't exist.

    Once you create it, it exists. Then you can get its path.


    -- Bill Stewart [Bill_Stewart]


    Friday, July 1, 2016 9:57 PM
    Moderator
  • Hi jrv,

    You are missing the fact that the file does not exist. Resolve-Path only works on existing files and directories. 

    Chris


    There is no such thing as a path to a file that doesn't exist.

    Join-Path $pwd $file

    This gets the only path that could be guessed at.


    \_(ツ)_/

    Friday, July 1, 2016 10:01 PM
  • Hello,

    Bill, as I explained in the first post, I have a function that will create a file in a path specified by the user. I have to have the full path of that file before it is created. Let's say that the user enters a path like "..\Documents\myfile.txt". I want to resolve that to "C:\Users\Username\Documents\myfile.txt" before creating the file.

    Jrv, Join-Path does not help since if the user enters a path like "..\Documents\myfile.txt" and the current path is "C:\Users\Username\Desktop" the result will be "C:\Users\Username\Desktop\..\Documents".

    Chris

    Saturday, July 2, 2016 9:28 AM
  • I have to have the full path of that file before it is created.

    I guess we were not successful in explaining why this is unnecessary. All you have to do is create the file. If you succeed, you can get the full path to it.


    -- Bill Stewart [Bill_Stewart]

    Saturday, July 2, 2016 6:36 PM
    Moderator
  • This is more an issue of clairvoyance or lack of it.  How can you know what the user has in mind when the file name is typed.  As a procedural and presentation issue this approach seems to fly in the face of all "best practices".  YOU are assuming that the user knows what the current folder is or that you know what the user thinks the current folder is.

    It would make more sense to display the current folder and allow the user to navigate or select the target.  This can easily be done with the OpenFileDialog.   This dialog does not assume a file exists and is much more user friendly.

    add-type -AssemblyName System.Windows.Forms
    $ofd=New-Object System.Windows.Forms.OpenFileDialog
    $ofd.CheckFileExists=$false
    $ofd.ShowDialog()

    The dialog can be optioned almost any way needed to assist the user and gain a good file path name.


    \_(ツ)_/

    Saturday, July 2, 2016 6:46 PM
  • I guess I should also note that you custom solution is good in that it does solve your immediate issue without further information or knowledge.  It is also a good exercise in building an Advanced Function.

    I suggest that we should all assume that such a trivial issue might have many solutions but I prefer solutions that already exist.  Your solution, to me, seems to be reinventing the wheel.  It is not wrong or bad but seeking a more general and complete solution seems a better choice.

    Also the ambiguity of the string returned from the user cannot be resolved in ny simple way.  A GUI, in this case, can solve all issues.  It will guarantee that a correct path is chosen and that the file name is parsed.  YOU can forse extensions and the folder view displayed.


    \_(ツ)_/

    Saturday, July 2, 2016 6:51 PM
  • Thank you very much for your help, I really appreciate it.

    Saturday, July 2, 2016 7:23 PM
  • Thank you very much for your help, I really appreciate it.


    We try to be helpful.  We are not always successful.  Many questions do not have exact answers in computing.

    \_(ツ)_/

    Saturday, July 2, 2016 7:46 PM