Dave Edmonds
Dave Edmonds

Reputation: 87

Powershell Array length

I have written the below to error trap an empty array and it isn't working. Any ideas on the syntax I need?

$inputstring =   "MyOtherFile.rdl" "MyFile.rdl"
$cleanstring =  $inputstring.replace(""" """,";")
$filearray = $inputstring.split(";")

if (echo @($filearray).length = "0")


{$filearray.length
'No Files Selected'

exit}

else
{$filearray.length}

It is returning 2 for the array length but is still tripping the 1st part of the IF and saying no files selected.

Upvotes: 2

Views: 11334

Answers (1)

derekbaker783
derekbaker783

Reputation: 9591

You could do something like this:

function ValidateArrayLength([string[]] $files) {
    if ($files.length -eq 0) {
        $files.length
        'No Files Selected'
        exit
    }
    else {
        $files.length
    }
}

$filearray = @("MyOtherFile.rdl", "MyFile.rdl")
ValidateArrayLength -files $filearray

$filearray = @()
ValidateArrayLength -files $filearray

Upvotes: 3

Related Questions