2015-03-02 61 views
0

我想使用Powershell腳本將文件夾遞歸複製到其他位置。這必須在PowerShell的TODO:如何使用Powershell腳本將數據從位置A複製到位置B?

  • 複製文件和文件夾從位置A到位置B
  • UNC路徑必須有(例如\ net.local \文件\ EDV)
  • 位置B必須全部爲空文件夾清除
  • 位置B的結構必須等於位置A
  • 應該在B上創建缺少的文件夾。
  • 應該只複製文件是老年人超過180天
  • 腳本必須創建包含文件名和路徑,文件大小信息的日誌文件,文件日期

我有這個劇本開始:

$a = '\\serverA\folderA' 
$b = '\\serverB\folderB' 

#This copies the files 
Get-ChildItem $a -Recurse -File | Foreach_Object {Copy-Item $_ -Destination $b} 

#Removes empty files 
Get-ChildItem $b -File | Foreach-Object {IF($_.Length -eq 0) {Remove-Item $_}} 

我需要幫助..

+1

考慮使用ROBOCOPY:https://technet.microsoft.com/de-de/library/cc733145%28v=ws.10%29.aspx – 2015-03-02 10:47:29

回答

1

這段代碼複製目錄到另一個目錄,剩下的應該直截了當。在$toreplace中,每個反斜槓都應該使用額外的反斜槓進行轉義。

$a = [System.IO.DirectoryInfo]'C:\Users\oudou\Desktop\dir' 
$b = [System.IO.DirectoryInfo]'C:\Users\oudou\Desktop\dir_copy' 


function recursive($a,$b) 
{ 
    foreach ($item in @(Get-ChildItem $a.FullName)) 
    { 
     if($item -is [System.IO.DirectoryInfo]) 
     { 
      if (-not (Test-Path $item.FullName.Replace($a.FullName,$b.FullName))) 
      { 
       New-Item -ItemType Directory $item.FullName.Replace($a.FullName,$b.FullName) 
      } 
      $dest = Get-ChildItem $item.FullName.Replace($a.FullName,$b.FullName) 
      $dest 
      recursive($item, $dest) 
     } 
     else 
     { 
      [string]$y = $item.FullName 
      $toreplace = "C:\\Users\\oudou\\Desktop\\dir" 
      $replace = "C:\Users\oudou\Desktop\dir_copy" 
      $y -replace $toreplace , $replace    
      Copy-Item $item.FullName ($item.FullName -replace $toreplace , $replace) 
     } 
    } 
} 


recursive $a $b 
相關問題