about_IOInfoExtensions.PowerShell_TryDelete.help.txt

TOPIC
    about_IOInfoExtensions.PowerShell_TryDelete
 
SHORT DESCRIPTION
    Deletes the file if it exists.
 
SYNTAX
    TryDelete()
 
LONG DESCRIPTION
    Attempts to delete the file. If the file or it's parent directory do not exist, no exception is thrown.
 
    [System.IO.FileInfo] has a 'Delete' method, but it throws an exception if the parent directory does not exist.
 
OUTPUTS
    N/A
 
EXAMPLES
    --------------------- Example 1 ---------------------
    Called on an existing file.
 
        $file = New-Object System.IO.FileInfo 'C:\Demo\ChildFile1.txt'
 
        Write-Host "File Information:"
        Write-Host (Get-FileTable -Files $file -IncludeDirectory)
 
        Write-Host "Deleting file..."
        $file.TryDelete()
 
        Write-Host "File Information after delete:"
        Write-Host (Get-FileTable -Files $file -IncludeDirectory)
 
        <#
        Output:
        File Information:
                Directory Name DirectoryExists Exists
                --------- ---- --------------- ------
                C:\Demo ChildFile1.txt True True
        Deleting file...
        File Information after delete:
                Directory Name DirectoryExists Exists
                --------- ---- --------------- ------
                C:\Demo ChildFile1.txt True False
        #>
 
    The file is deleted and the [FileInfo] object is refreshed.
 
    Notice:
    - Using the 'Delete' method, the final 'Exists' call would have returned "true" until a 'Refresh()' is called.
 
 
    --------------------- Example 2 ---------------------
    Called on a non-existing file but parent directory does exist.
 
        $file = New-Object System.IO.FileInfo 'C:\Demo\ChildFile4.txt'
 
        Write-Host "File Information:"
        Write-Host (Get-FileTable -Files $file -IncludeDirectory)
 
        Write-Host "Deleting file..."
        $file.TryDelete()
 
        Write-Host "File Information after delete:"
        Write-Host (Get-FileTable -Files $file -IncludeDirectory)
 
        <#
        Output:
        File Information:
                Directory Name DirectoryExists Exists
                --------- ---- --------------- ------
                C:\Demo ChildFile4.txt True False
        Deleting file...
        File Information after delete:
                Directory Name DirectoryExists Exists
                --------- ---- --------------- ------
                C:\Demo ChildFile4.txt True False
        #>
 
    No exception is thrown and the [FileInfo] object is unchanged.
 
    Notice:
    - Using the 'Delete' method would have had the exact same results in this scenario.
 
 
    --------------------- Example 3 ---------------------
    Called on a non-existing file and parent directory does not exist.
 
        $file = New-Object System.IO.FileInfo 'C:\Demo\ChildDir4\ChildFile4.txt'
 
        Write-Host "File Information:"
        Write-Host (Get-FileTable -Files $file -IncludeDirectory)
 
        Write-Host "Deleting file..."
        $file.TryDelete()
 
        Write-Host "File Information after delete:"
        Write-Host (Get-FileTable -Files $file -IncludeDirectory)
 
        <#
        Output:
        File Information:
                Directory Name DirectoryExists Exists
                --------- ---- --------------- ------
                C:\Demo\ChildDir4 ChildFile4.txt False False
        Deleting file...
        File Information after delete:
                Directory Name DirectoryExists Exists
                --------- ---- --------------- ------
                C:\Demo\ChildDir4 ChildFile4.txt False False
        #>
 
    No exception is thrown and the [FileInfo] object is unchanged.
 
    Notice:
    - Using the 'Delete' method would have caused an exception to be thrown.
 
 
KEYWORDS
    IOInfoExtensions, IOInfoExtensions.PowerShell, System.IO.FileInfo, FileInfo, TryDelete
 
SEE ALSO
    about_IOInfoExtensions.PowerShell
    about_IOInfoExtensions.PowerShell_GetDirectory
    about_IOInfoExtensions.PowerShell_GetFile
    about_IOInfoExtensions.PowerShell_DeleteContent
    about_IOInfoExtensions.PowerShell_CopyContentTo
    about_IOInfoExtensions.PowerShell_MoveFrom
    about_IOInfoExtensions.PowerShell_CopyFrom
    about_IOInfoExtensions.PowerShell_TryDelete