Share via

ROBOCOPY Command not idetifying Powershell Variable way of source & Destination

VijAz 161 Reputation points
2021-01-20T12:55:07.91+00:00
$Bpspath = "$BpsPath_1\$dir\$Number\Full\AG"
                    $filename = gci -Path $("$BpsPath\$db")  |  Where-object {$_.Name -match $datefilter} | select -ExpandProperty name 
                    $filename

                    $source = "$BpsPath\$db" 
                    $dest   = "$Destination\$db"

                    #starting job for every item in source list

                    ROBOCOPY "$source" "$dest" "$filename" /NP /TS /FP  #| Out-File $outfile -Append

In the ROBOCOPY command , the source file only identified correctly , where as the $dest and $filename are not identified correctlt. BUT when checked the actual values of these $dest and $filename , they look correct. Please help me understand what is fault here.

Windows for business | Windows Server | User experience | PowerShell

1 answer

Sort by: Most helpful
  1. Michael Taylor 61,226 Reputation points
    2021-01-20T15:48:24.127+00:00

    If there is more than one matching file then $filename is an array, not a single string. The command wouldn't know how to handle an array and therefore would simply create the directory structure.

    To get this to work you need to flatten the array into a space separated string of filenames. The splat operator (@) can be used for that.

    robocopy "$source" "$dest" @filename /np /ts /fp
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.