0

我对PS有点新笨。我已经完成了这里记录的所有问题,并尽我所能为我工作,但不幸的是并没有像我想要的那样为我工作。

我试图复制整个文件夹结构,但不包括一些。例如,我想将整个 c:\Program Files\myTest 复制到目标 C:\Program Files\TargetTest (不包括以下文件夹) c:\Program Files\myTest\_Backup 和 c:\Program Files\myTest\Processor\Reports

     $SourceFolder = "$env:ProgramFiles\myTest\"
     $ReportExclude="$env:ProgramFiles\myTest\Processor\Reports\*.rdl"
     $TargetFolder = "$env:ProgramFiles\TargetTest"
     $BackupRootFolderName = "_Backup"
     $BackupRootFolderPath = "$TargetFolder\$BackupRootFolderName"
     $BackupFolderPath = "$BackupRootFolderPath\$(Get-Date -format yyyyMMdd_HHmmss)"

      Get-Item -Path $SourceFolder\* -Exclude $ReportExclude, _Backup| %  { write-host $_.fullname; Copy-Item -path $_.fullname -destination $TargetFolder -recurse}

当我运行上面的代码时,它会复制所有内容并排除唯一的 _Backup 但无论如何都会复制 Reports 文件夹/文件,我不想被复制。

有人可以调查一下并指出我做错了什么。

非常感谢

4

2 回答 2

0

解决方案并不那么容易,-Exclude 开关排除文件夹但不排除子项目,-Filter 开关使用通配符但将单个字符串作为参数..(顺便说一句,@whatever 在他说 Copy-recurse 开关时是正确的-项目取消您对 Get-ChildItems 的排除)

最后我以一种肮脏但有效的方式解决了它:

Get-ChildItem -Path $SourceFolder -Recurse | ? { $_.FullName -notlike $ReportExclude -and $_.FullName -notlike $BackupRootFolderName } | % { write-host $_.FullName; Copy-Item -Path $_.FullName -Destination $($_.FullName -replace $SourceFolder, $TargetFolder) -Force} 

编辑,因为我不明白你的配置中有什么不工作,让我们用 loggin 测试一个完整的脚本:

$SourceFolder = "$env:ProgramFiles\myTest\" 
$ReportExclude = "$env:ProgramFiles\myTest\Processor\Reports\*.rdl" 
$TargetFolder = "$env:ProgramFiles\TargetTest" 

$BackupRootFolderName = "_Backup" 
$BackupRootFolderPath = "$TargetFolder\$BackupRootFolderName" 
$BackupFolderPath = "$BackupRootFolderPath\$(Get-Date -format yyyyMMdd_HHmmss)" 

$ErrorActionPreference = "Continue"
$WarningPreference = "Continue"
$VerbosePreference = "Continue"

Function Copy-MyFiles($FileFullNameSource)
{
    try
    {
        Write-Verbose "file source : $FileFullNameSource"

        if ($FileFullNameSource -notlike $ReportExclude -and $FileFullName -notlike $BackupRootFolderName)
        {
            $FileFullNameDestination = $FileFullNameSource -replace $SourceFolder, $TargetFolder

            Write-Verbose "file destination : $FileFullNameDestination"

            Copy-Item -Path $FileFullNameSource -Destination $FileFullNameDestination -Force

            if (Test-Path $FileFullNameDestination)
            {
                Write-Host "$FileFullNameDestination ok" -ForegroundColor Green
            }
            else
            {
                Write-Warning "$FileFullNameDestination is missing in destination folder"
            }
        }
        else
        {
            Write-Host "$FileFullNameSource is excluded"
        }
    }
    catch
    {
        Write-Warning ($_.Exception | Select-Object -Property Message |  Out-String)
    }
}

Write-Verbose "source folder : $SourceFolder"
Write-Verbose "target folder : $TargetFolder"

Get-ChildItem -Path $SourceFolder -Recurse | % { Copy-MyFiles $_.FullName }
于 2016-12-01T14:35:42.797 回答
0

您的问题是 copy-item 语句中的 -recurse 开关。 Get-Item 将为该命令提供文件夹“$env:ProgramFiles\myTest\”,

Copy-Item -Recurse

然后将复制该文件夹,包括所有子文件夹。尝试这个:

Get-ChildItem -Path $SourceFolder\* -Exclude $ReportExclude, _Backup -Recurse | %  { write-host $_.fullname; Copy-Item -path $_.fullname -destination $TargetFolder} 

这应该首先生成所有文件夹和文件的列表(包括子文件夹),忽略您要排除的那些,然后将它们中的每一个复制到目标目的地。
当。一旦我有时间,将检查适当的解决方案。

于 2016-12-01T14:00:27.297 回答