复制项目不包括 PowerShell 中源目录的子文件夹文件

Copy-Item not including subfolder files of source directory in PowerShell

我有一个脚本可以将具有特定扩展名的文件从源目录移动到目标目录。

我的源目录如下所示:

文件夹

我的脚本很短,看起来像:

if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
    Write-Host "The source- or destination path is incorrect, please check "
    break
}else
{
    Write-Host "Success"
    Copy-Item -path $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath -Recurse
}

我不得不提到 $SourceDirPath 来自一个配置文件,只有当我将它声明为 C:\FILESFOLDER\*

时才有效

该脚本可以运行,但不会将文件从子文件夹复制到目标位置。目标只有 File1 和 File2。

我的 Copy-Item 命令有什么问题?

您应该使用带有 -recurse 参数的 Get-ChildItem cmdlet 来根据您的过滤条件检索所有文件。然后,您可以将结果通过管道传输到 Copy-Item cmdlet,只需指定一个目的地。

所以这将比较目录源和目标,然后复制内容。忽略我的时髦变量,我在下面的编辑中修改了它们

 #Release folder
    $Source =  ""
    #Local folder
    $Destination = ""

    #Find all the objects in the release
    get-childitem $Source -Recurse | foreach {

    $SrcFile = $_.FullName
    $SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5 # Obtain hash

    $DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination #Escape the hash
    Write-Host "comparing $SrcFile to $DestFile" -ForegroundColor Yellow

    if (Test-Path $DestFile) 
    {
    #Check the hash sum of the file copied
    $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5

    #compare them up
    if ($SrcHash.hash -ne $DestHash.hash) {
        Write-Warning "$SrcFile and $DestFile Files don't match!"

        Write-Warning "Copying $SrcFile to $DestFile " 

        Copy-Item $SrcFile -Destination $DestFile -Force
    } 
    else {
        Write-Host "$SrcFile and $DestFile Files Match" -ForegroundColor 
Green

    }
        } 
    else {
        Write-host "$SrcFile is missing! Copying in" -ForegroundColor Red
        Copy-Item $SrcFile -Destination $DestFile -Force

         }
    }  

-Recurse 开关对您的情况没有影响,因为它只应用于与 $SourceDirPath 组合匹配的项目,它使用尾随 \* 以匹配 源目录中的项目,以及 -Filter,在您的情况下只是位于 *.td 文件]直接在您的源目录中。

省略尾随 \* 以指向目录本身(例如,C:\FOLDER 而不是 C:\FOLDER\*),解决了这个问题 原则上,但是,due to an annoying quirk 只有在目标目录存在的情况下才能正常工作。 如果它确实存在,项目将被放置在目标目录的 子目录 中,以 source 目录命名。

如果复制前现有目标目录中没有任何内容需要保留(并且目录本身没有特殊属性需要保留,您可以通过以下方式解决问题删除预先存在的目标目录

if(!(Test-Path $SourceDirPath) -or !(Test-Path $DestinationDirPath))
{
    Write-Host "The source or destination path is incorrect, please check "
    break
}
else
{
    Write-Host "Success"

    # Delete the preexisting destination directory,
    # which is required for the Copy-Item command to work as intended.
    # BE SURE THAT IT IS OK TO DO THIS.
    Remove-Item $DestinationDirPath -Recurse

    # Note: $SourceDirPath must NOT end in \*  
    Copy-Item -Recurse -LiteralPath $SourceDirPath -Filter "*$extension" -Destination $DestinationDirPath
}

如果您确实需要保留现有的 $DestinationDirPath 内容或属性(ACL,...),则需要做更多的工作。

试试这个:

$source =  "C:\Folder1"

$destination = "C:\Folder2"

$allfiles = Get-ChildItem -Path $source -Recurse -Filter "*$extension"
foreach ($file in $allfiles){
    if (test-path ($file.FullName.replace($source,$destination))) {
    Write-Output "Seccess"
    Copy-Item -path $file.FullName -Destination ($file.FullName.replace($source,$destination))
    }
    else {
    Write-Output "The source- or destination path is incorrect, please check "
    break
    }
}