Robocopy - получить имена файлов скопированных файлов и добавить их в список - PullRequest
1 голос
/ 06 февраля 2020

Я пытаюсь скопировать указанные c файлы из недавно созданных папок на мой локальный диск, и я хотел бы очистить целевой каталог, удалив все файлы, кроме тех, которые были скопированы с помощью robocopy.

$source = "\\server\Builds\Installations\1.0"
$target = "\\c$\temp\Engineering\1.0"
$artifactsToMirror = "*.zip*"
$numberOfBuildsToMirror = 2
$doCleanUp = "true"
$filesToKeep = New-Object System.Collections.Generic.List[System.Object]

try
{   
    if (-Not (Test-Path -PathType Container $target))
    {
       Write-Output "Creating target directory..."
       New-Item -ItemType Directory -Force -Path $target
    }

    Get-ChildItem -Path $source -Directory | Sort-Object LastWriteTime | Where-Object { 
    ($_.GetFiles().Count -gt 0) } | Select-Object -Last $numberOfBuildsToMirror | ForEach-Object `
    {
        robocopy $source\$_ $target $artifactsToMirror /NJH /NJS /FP /NS /NC /NP /NDL /W:0 | ForEach-Object { $filesToKeep.Add($_)}
    }
    #Clean up target directory
    if ($doCleanUp -eq [bool]::TrueString -and $filesToKeep.Count -gt 0)
    {
       Write-Output "Cleaning up target directory"
       Remove-Item $target\* -recurse -exclude $filesToKeep -Force
    }   
}
catch [Exception]
{
   Write-Output "Not able to mirror the builds"
   Write-Output $_.Exception
   exit 1
}
...