2

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

我的源目录如下所示:

文件夹

  • 文件1.td

  • 文件2.td

    子文件夹

    • 文件3.td

    • 文件4.td

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

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 命令有什么问题?

4

4 回答 4

3

-Recurse开关对您的情况无效,因为它仅适用于由 组合匹配的项目,该组合使用$SourceDirPath尾随\*匹配源目录中的项目,并且在您的情况下仅是直接位于源中的文件目录。-Filter*.td

省略结尾\*以指向目录本身(例如,C:\FOLDER而不是),原则上C:\FOLDER\*解决了该问题,但是,由于烦人的怪癖,只有在目标目录尚不存在时才能按预期工作。 如果确实存在,则将项目放置在目标目录的子目录中,以目录命名。

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

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,...),则需要做更多工作。

于 2017-04-13T13:00:23.790 回答
1

尝试这个:

$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
    }
}
于 2017-04-13T13:01:22.747 回答
1

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

于 2017-04-13T12:24:35.393 回答
1

因此,这将比较目录源与目标,然后复制内容。忽略我的时髦变量,我在下面修改了它们

 #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

         }
    }  
于 2017-04-13T12:45:53.663 回答