0

I'm trying to create a function which takes two parameters and do some repetitive tasks, but the execution fails : Here is the function :

Function RenameFolderFiles{
    param(
        [String[]]$FldPath, [String]$TypeFld
    )
    
    Write-Host $TypeFld " : " $FldPath.Length
    Write-Host $FldPath
    for ( $i = 0; $i -lt $FldPath.Length; $i++ ) {
        write-host $i " : " $FldPath.FullName[$i]
        $NewFld = ([string]$FldPath.FullName[$i]).Replace(" _ tt","");
    }
}

I call the function like this :

$Fld = Get-ChildItem -Path "$varCheminRepertoireScript" -Recurse -Directory | Where-Object {$_.FullName -like "* _ tt*"}

RenameFolderFiles -FldPath $Fld -TypeFld "Nb of Folders & Sub-Folders"

But the result is :

.
.
.
Cannot index into a null array.
At C:\Test.ps1:10 char:9
+         write-host $i " : " $FldPath.FullName[$i]
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

What is wrong with the parameters / types passed ? Thanks for your clarifications !! BR

3
  • What's the last output written to the screen immediately before the error is thrown? Does it happen on the first iteration, or later? Commented Sep 30, 2022 at 9:40
  • Thanks Mathias, It happens on the first iteration. Here is the result ``` Nb of Folders & Sub-Folders : 24 Conve... #here is the printout of $FldPath which is too long for this response Cannot index into a null array. At C:\Files_2022\Privé\PowerShell\Rename_s-lib.org_files_S_220930.ps1:67 char:9 + write-host $i " : " $FldPath.FullName[$i] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray And the rest of the output is similar for all 24 values Commented Sep 30, 2022 at 11:25
  • Are you by any chance using PowerShell 2.0 or 3.0? You can check the version by inspecting $PSVersionTable Commented Sep 30, 2022 at 11:37

1 Answer 1

1

There is No property like FullName on string[] $FldPath or on $FldPath[0] index Item $FldPath.FullName

set [object[]]$FldPath instead of [String[]]$FldPath in param of Function

Sign up to request clarification or add additional context in comments.

4 Comments

I think you're right, but your answer is poor with a statement as "Check your code again" (it is more like a comment). Please add some explanation and background to it.
Thanks ABC DEF for the answer. But when I check the $FldPath.Length, it reports 24 (in my test). How can I address these 24 different values in $FldPath ?
@StartingPS $FldPath[$i].FullName
set [object[]]$FldPath instead of [String[]]$FldPath in param of Function

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.