使用copy-item命令时,Powershell不排除特定的子文件夹

时间:2016-12-01 13:49:42

标签: powershell powershell-v4.0 copy-item

我在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文件夹/文件,我不想复制它。

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

非常感谢

2 个答案:

答案 0 :(得分:0)

您的问题是copy-item语句中的-recurse开关。     GET-项目 将给该命令文件夹“$ env:ProgramFiles \ myTest \”,

Copy-Item -Recurse
然后

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

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

这应首先生成所有文件夹和文件的列表(包括子文件夹),忽略要排除的文件夹,然后将每个文件夹和文件复制到目标目标。
宕。一旦我有时间,就会检查一个合适的解决方案。

答案 1 :(得分:0)

解决方案并不那么容易,-Exclude开关排除文件夹而不是子项,-Filter开关使用通配符但是将一个字符串作为参数..(顺便说一句@whatever是正确的,当他说-recurse开关在Copy-Item上取消对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 }