[SOLVED]-Logic problem with my code or bug?

Hi all,

I have a real strange one here. I have a script that simply copies some files from one location to a number of other subsequent locations. The first time I run the script everything works as expected but if I run it again it creates an additional folder and I do not understand why? It seems Copy-Item works as intended the first time (to copy the contents of a folder to a destination) but if I run it a second time it copies the folder object itself too?

[pre]
$Path = ‘C:\Users\Public\Solibri\SMC*’
$ScriptLocation = (Split-Path $MyInvocation.MyCommand.Path) #Get location of running script

if (Test-Path $Path.Trim()) {
$Destination = Get-ChildItem $Path | Select-Object -ExpandProperty FullName #Get the fullname of the wildcard folders

$Destination | ForEach-Object {
    $_ += '\RuleSets\FolderToCopy' #Add additional directories to $Destination
    Copy-Item $ScriptLocation\FolderToCopy $_ -Recurse -Force -ErrorAction Stop #Make 
}

}
[/pre]

Thanks for any help you can provide - I have had 3 people look at this so far and cannot work it out.

Hi,

Can you clarify what do you mean by “copies folder object itself too”? I don’t understand what is the difference in copying a folder vs folder object.

Thanks

Hi EdgarD17,

So the first time the script is run ‘Copy-Item’ only copies the contents of the source directory into the destination directory but if I run the script again then ‘Copy-Item’ seems to copy the source directory object itself (+ the contents of it), so I end up like this:

First Time Script Is Run:

C:\Users\Public\Solibri\SMCv9.6\RuleSets\FolderToCopy\source folder contents

Second Time Script Is Run (on the same machine):

C:\Users\Public\Solibri\SMCv9.6\RuleSets\FolderToCopy<strong>SourceFolder\source folder contents

I get an extra ‘SourceFolder’ and not sure why?

Apologies all, I thought ‘Copy-Item’ only copies the contents of the source directory but it appears that is not the case, it copies the source directory object + all contents.