Home > Software design >  Check if function input is a System.IO.FileInfo or a string object?
Check if function input is a System.IO.FileInfo or a string object?

Time:02-05

This seems to work on my Terminal, It will correctly Identify the Object Type:

If ($var.getType().name -eq 'DirectoryInfo'){'File IO Obj'}else{'String Obj'}

But inside a function it falls apart:

Function Test{
    param(
        [Parameter(ValueFromPipeline, Position = 1)]
        $InputObect
        )
            If ($input.getType().name -eq 'DirectoryInfo')Else{'Something Else'}
}

I tried this as well:

Function Test{
    param(
        [Parameter(ValueFromPipeline, Position = 1)]
        $InputObect
        )
        $global:var = $InputObect
}

Checking on terminal the type of $Var with $var.getType().FullName returns System.Object[]

I have tried searching but this is all I could turn up.

I am simply looking to identify the type of file piped/sent in, then use If or Else to do something.

Any help would be greatly appreciated!

CodePudding user response:

You can do it like this:

function Test {
    Param (
        [System.IO.FileInfo]$FileObject
    )

    Write-Host ($FileObject -eq $null)
    if ($FileObject -eq $null) {
        return $null
    }

    if ($FileObject.GetType() -eq [System.IO.FileInfo]) {
        Write-Host ("Input is file object")
    }

    Write-Host ($FileObject.FullName)
}

Test -FileObject ([System.IO.FileInfo]::new("C:\users\user\documents\file.txt"))

Define the required type within in the parameter declaration to make it easy to understand what is required.

If you want to accepted multiple types you can do it like this:

function Test {
    Param (
        $IoObject
    )

    if ($IoObject -eq $null) {
        return $null
    }
    
    if (($IoObject.GetType() -eq [System.IO.FileInfo]) -or ($IoObject.GetType() -eq [System.IO.DirectoryInfo])) {
        Write-Host ("Input is file object or directory object")
    }
}

You could also check for GetType().BaseType -eq [System.IO.FileSystemInfo] which should be true for Directories and Files as well.

CodePudding user response:

Another way to address this use case.

Getting the object type directly - for example select the first object returned for a directory or a file:

# Directory
((Get-ChildItem -Path 'D:\Temp' -Directory)[0]).GetType().Name
# Results
<#
DirectoryInfo
#>

# File
((Get-ChildItem -Path 'D:\Temp' -File)[0]).GetType().Name
# Results
<#
FileInfo
#>

Then just directly compare the results in one line

# Folder and file
(((Get-ChildItem -Path 'D:\Temp' -Directory)[0]).GetType().Name).CompareTo(((Get-ChildItem -Path 'D:\Temp' -File)[0]).GetType().Name)
# Results
<#
-1
#>

File and file

(((Get-ChildItem -Path 'D:\Temp' -File)[0]).GetType().Name).CompareTo(((Get-ChildItem -Path 'D:\Temp' -File)[0]).GetType().Name)
# Results
<#
0
#>

As a full function

Clear-Host
Function Test-InputObjectType
{
    <#
    .Synopsis
       Short description

    .DESCRIPTION
       Long description

      # Use this as an example of case sensitive string matching

      .EXAMPLE
       Example of how to use this function

    .EXAMPLE
       Another example of how to use this function
    #>
    
    [CmdletBinding(SupportsShouldProcess)]
    [Alias('tiot')]

    param
    (
        [Parameter(ValueFromPipeline, 
        Position = 1)]
        $InputObject
    )

    # Initialize GUI resources
    # ...

    # Required for use with web SSL sites
    # ...

    #region       Begin Script ---
    #
    If  ((((Get-ChildItem -Path $InputObject -Directory)[0]).GetType().Name) -eq 'DirectoryInfo')
        {"$InputObject is of type DirectoryInfo"}
    Else {"$InputObject is of type FileInfo"}

    #
    #endregion    EndScript --- 
}

Test-InputObjectType -InputObject 'D:\Temp'
# Results
<#
D:\Temp is of type DirectoryInfo
#>


Test-InputObjectType -InputObject 'D:\Temp\arp_IPA.txt'
# Results
<#
D:\Temp\arp_IPA.txt is of type FileInfo
#>
  • Related