2015-03-02 45 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 
相关问题