1

This should process each sub-folder . I've already spent alot of time on this, and would appreciate help .

The output makes no sense to me


function sc_t ([Parameter(ValueFromPipeline)]$path = "") {
process {

    function sc1 ([Parameter(ValueFromPipeline)]$sub_psobject, $sub_targetPath = "") {
    process {
        
        if ($sub_psobject.PSIsContainer) {
            
            $sub_psobject.fullname
            "$sub_targetPath\$($sub_psobject.PSChildName)"
            
            get-childItem -Force $sub_psobject | sc1 -sub_targetPath "$sub_targetPath\$($sub_psobject.PSChildName)"
        }
        
    }
    }
    
    $psobject = get-item -Force -LiteralPath $path
    sc1 $psobject

}
}

sc_t "D:\temporal\1"
irvnriir
  • 673
  • 5
  • 14
  • The problem is that _Windows PowerShell_ sometimes stringifies `Get-ChildItem` output objects by file _name_ only rather than by _full path_, _depending on how `Get-ChildItem` was invoked_; the problem has been fixed in _PowerShell (Core) v6.1+_. The workaround is to use `.FullName` to ensure use of the full path. See [this answer](https://stackoverflow.com/a/53400031/45375) for details. – mklement0 Dec 30 '21 at 01:09
  • As an aside: You're implicitly creating an [advanced](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Functions_Advanced) function by using `[Parameter()]` attributes. In terms of syntax, in advanced functions it is preferable to use the body-internal `param(...)` block for parameter declarations, for the reasons explained in [this answer](https://stackoverflow.com/a/61946390/45375). – mklement0 Dec 30 '21 at 01:21

1 Answers1

1

When i run your process, it gives errors on each sub folder. Noticed that each Get-ChildItem is using only the sub path.. not full path.

Change your Get-ChildItem line to use fullname ... should work.

# instead of 
get-childItem -Force $sub_psobject | ...

# do
get-childItem -Force $sub_psobject.fullname

Also.. remark that second line in your sc1 function.. produces duplicates.

"$sub_targetPath\$($sub_psobject.PSChildName)"

complete code

function sc_t ([Parameter(ValueFromPipeline)]$path = "") {
  process {
    function sc1 ([Parameter(ValueFromPipeline)]$sub_psobject, $sub_targetPath = "") {
      process {
        if ($sub_psobject.PSIsContainer) {
            $sub_psobject.fullname            
            get-childItem -Force $sub_psobject.fullname | sc1 -sub_targetPath "$sub_targetPath\$($sub_psobject.PSChildName)"
        }
      }
    }
    $psobject = get-item -Force -LiteralPath $path
    sc1 $psobject
  }
}
Jawad
  • 11,028
  • 3
  • 24
  • 37