2012-02-14 11 views
6

मैं एक बिन फ़ोल्डर (OutputDir/बिन) कुछ फ़ाइलें जो OutputDir में रहने के अलावा में सभी निर्माण उत्पादन फ़ाइलों और फ़ोल्डरों को कॉपी करने की कोशिश कर रहा हूँ ले जाएँ। बिन फ़ोल्डर कभी नहीं हटाया जाएगा।Powershell: फ़ाइलें रिकर्सिवली

प्रारंभिक शर्त:

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

लक्ष्य:

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

मेरे पहली कोशिश:

$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 

यह कोई करता है टी काम, क्योंकि Move-Item फ़ोल्डर को ओवरराइट करने में सक्षम नहीं है।

मेरी दूसरी कोशिश:

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 का उपयोग कर एक और अधिक आसान तरीका में रिकर्सिवली फ़ाइलों को ले के किसी भी वैकल्पिक रास्ता नहीं है?

+0

आप इसे खत्म करना चाहते हैं क्योंकि इससे आपको आवश्यक उत्तर प्राप्त करने में मदद मिलेगी। –

उत्तर

6

आप एक Copy-Item कमांड के साथ Move-Item आदेश की जगह सकता है और उसके बाद, आप बस बुला Remove-Item द्वारा फ़ाइलों को आप ले जाया हटा सकते हैं: आप यह कैसे का एक उदाहरण फ़ोल्डर संरचना और उसके बाद कैसे दिखाते हैं तो

$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

आपके लिए उपरोक्त, केवल नकारात्मक है आपका फ़िल्टर केवल रूट में आइटम पर लागू होता है, और फ़िल्टर को पुनरावर्ती रूप से लागू नहीं करता है। –

0

जैसा कि पहले से ही कहा गया है, मूव-आइटम फ़ोल्डर को ओवरराइट नहीं करेगा, इसलिए आपको कॉपी करने के साथ छोड़ दिया गया है। एक वैकल्पिक समाधान प्रत्येक फ़ाइल के लिए/प्रत्येक एमओवी स्विच (दूसरों के बीच) के साथ प्रत्येक फ़ाइल के लिए रोबोकॉपी को कॉल करना होगा; यह आगे बढ़ जाएगा स्रोत फ़ाइल को हटा दें।

संबंधित मुद्दे