2012-02-14 10 views
6

Dosyalar taşıyın. Bin klasörü asla silinmeyecek.Powershell: Ben <strong>outputdir</strong> kalmak bazı dosyaların dışında bir <strong>Bin</strong> klasöründe (<strong>outputdir/Bin</strong>) içine tüm yapı çıktı dosyaları ve klasörleri kopyalamak çalışıyorum yinelemeli

Başlangıç ​​durumu:

Output 
    config.log4net 
    file1.txt 
    file2.txt 
    file3.dll 
    ProjectXXX.exe 
    en 
     foo.txt 
    fr 
     foo.txt 
    de 
     foo.txt 

Hedef:

Output 
    Bin 
     file1.txt 
     file2.txt 
     file3.dll 
     en 
     foo.txt 
     fr 
     foo.txt 
     de 
     foo.txt 
    config.log4net 
    ProjectXXX.exe 

İlk deneyin:

$binaries = $args[0] 
$binFolderName = "bin" 
$binFolderPath = Join-Path $binaries $binFolderName 

New-Item $binFolderPath -ItemType Directory 

Get-Childitem -Path $binaries | ? {$_.Name -notlike "ProjectXXX.*" -and $_.Name -ne "config.log4net" -and $_.Name -ne $binFolderName } | Move-Item -Destination $binFolderPath 

Bu, hiçbir yapar Çalışır, çünkü Move-Item, klasörlerin üzerine yazamaz.

Benim ikinci deneyin:

function MoveItemsInDirectory { 
    param([Parameter(Mandatory=$true, Position=0)][System.String]$SourceDirectoryPath, 
      [Parameter(Mandatory=$true, Position=1)][System.String]$DestinationDirectoryPath, 
      [Parameter(Mandatory=$false, Position=2)][System.Array]$ExcludeFiles) 
    Get-ChildItem -Path $SourceDirectoryPath -Exclude $ExcludeFiles | %{ 
     if ($_ -is [System.IO.FileInfo]) { 
      $newFilePath = Join-Path $DestinationDirectoryPath $_.Name 
      xcopy $_.FullName $newFilePath /Y 
      Remove-Item $_ -Force -Confirm:$false 
     } 
     else 
     { 
      $folderName = $_.Name 
      $folderPath = Join-Path $DestinationDirectoryPath $folderName 

      MoveItemsInDirectory -SourceDirectoryPath $_.FullName -DestinationDirectoryPath $folderPath -ExcludeFiles $ExcludeFiles 
      Remove-Item $_ -Force -Confirm:$false 
     } 
    } 
} 

$binaries = $args[0] 
$binFolderName = "bin" 
$binFolderPath = Join-Path $binaries $binFolderName 
$excludeFiles = @("ProjectXXX.*", "config.log4net", $binFolderName) 

MoveItemsInDirectory $binaries $binFolderPath $excludeFiles 

PowerShell kullanarak daha kolay bir şekilde yinelemeli dosyaları hareketli herhangi bir alternatif yolu var mı?

+0

Sonuna ihtiyacınız olsun, size ihtiyacınız olan cevabı almanıza yardımcı olur. –

cevap

6

Bir Copy-Item komutla Move-Item komutu yerini alabilecek ve bundan sonra, sadece Remove-Item arayarak Taşıdığınız dosyaları silebilirsiniz: Eğer o zaman nasıl bir örnek klasör ne kadar yapısı ve gösterirse

$a = ls | ? {$_.Name -notlike "ProjectXXX.*" -and $_.Name -ne "config.log4net" -and $_.Name -ne $binFolderName } 
$a | cp -Recurse -Destination bin -Force 
rm $a -r -force -Confirm:$false 
+0

Sadece sizin için olumsuz, filtreniz yalnızca root'daki öğeler için geçerlidir ve filtreyi tekrar tekrar uygulamamaktadır. –

0

Daha önce Taşı-Öğe Eğer kopyalama ile kalacaksın böylece klasörleri üzerine yazmaz, belirtti. Alternatif bir çözüm, her bir döngüde her bir dosya için/MOV anahtarıyla (diğerleri arasında!) Robocopy çağırmak olacaktır; Bu işlem daha sonra kaynak dosyayı siler.

İlgili konular