2

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.

4
  • Hi Jon, do you have any explanation as to why you edited my question please? Commented May 11, 2020 at 10:53
  • it doesn't throw an error when called from inside the source system I am using. It is an array of all the files currently selected - yes I have executed it. Commented May 11, 2020 at 10:59
  • 3
    Powershell's comparison operators are -eq, -ne, -gt and so on. Commented May 11, 2020 at 10:59
  • Your code is not PowerShell. Is it possible to run PowerShell Core on your system? I am a bit confused why you would use echo in a condition. Otherwise, the equality operator for bash is ==. Commented May 11, 2020 at 11:49

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.