简体   繁体   English

Powershell复制项未复制

[英]Powershell Copy-Item not copying

I have to following directory structure: 我要以下目录结构:

fold1
 - file1
 - file2

fold2
 - file1

I am trying to test to see if the folders are identical, and if they arent, copy fold1 to fold2 and overwrite any different files. 我正在尝试测试文件夹是否相同,如果它们不相同,请将fold1复制到fold2并覆盖所有其他文件。 This is what I have tried: 这是我尝试过的:

$fold1 = Get-ChildItem -Recurse -path C:\fold1
$fold2 = Get-ChildItem -Recurse -path C:\fold2
$isDif = Compare-Object $fold1 $fold2
if ($isDif -eq $null) {
    Write-Host "Folders Are Identical"
}
else {
    Write-Host "Folders Are Different"
    Copy-Item -Path $fold1.FullName -Destination $fold2.FullName -Recurse -Force
}

But when I run it, it says the folders are different, but it doesn't copy anything over. 但是当我运行它时,它说文件夹是不同的,但是它不会复制任何内容。 No errors or anything, it just doesn't work. 没有错误或其他任何东西,只是行不通。

This is how I would recommend doing the task in powerhsell. 这就是我建议在powerhsell中执行任务的方式。 It makes it much easier if you create path variables, that way you are not trying to insert records into a directory object. 如果您创建路径变量,则可以轻松得多,这样就不会尝试将记录插入目录对象中。

$path1 = "C:\fold1"
$path2 = "C:\fold2"
$fold1 = Get-ChildItem -Recurse -path $path1
$fold2 = Get-ChildItem -Recurse -path $path2
$isDif = Compare-Object $fold1 $fold2
if ($isDif -eq $null) {
    Write-Host "Folders Are Identical"
}
else 
{
    Write-Host "Folders Are Different"
    ForEach($file in $fold1)
    {
        if($fold2 -notcontains $file)
        {
            Copy-Item -Path $file.FullName -Destination $path2 -Recurse -Force
        }
    }
}

i, just do it 我,就去做

 $path1 = "C:\temp2\*"
 $path2 = "C:\temp3"

 Copy-Item -Path $path1 -Destination $path2 -Recurse -Force -ErrorAction Continue

我最终使用robocopy代替:

robocopy c:\fold1 c:\fold2 /s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM