递归复制一个目录,但不包括子目录及其CONTENT



我找到了解决方案,但是这个解决方案不排除被排除的子目录的内容。

我想递归地复制一个目录到另一个位置,但不包括子目录"视频"及其内容。下一个命令结束,但不排除子目录"Videos"及其内容。

# PowerShell version: 4.0
# This command copy recursively the directory "J:All" to "J:UsersJohnDesktopmy_backup", excluding "J:AllVideos"
Copy-Item -Recurse -Path ("J:All" | ? { $_.FullName -NotMatch ("^J:\All\Videos") }) "J:UsersJohnDesktopmy_backup"

谢谢!

RoboCopy是一个更好的解决方案。见https://superuser.com/questions/482112/using-robocopy-and-excluding-multiple-directories

robocopy J:All J:UsersJohnDesktopmy_backup /MIR /XD j:allvideos

试试这个:

$Source = 'J:All'
$Destination = 'J:UsersJohnDesktopmy_backup'
$Exclude = 'J:AllVideos'
Get-Childitem -Recurse -Path $Source | 
Where-Object  { $Exclude -notcontains $_.Directory } |
foreach {
 $TrimmedPath = ($_.fullname -replace [regex]::escape($Source)).trim('') |
 copy-item  -Destination "$($Destination.Trim(''))$TrimmedPath"
}

最新更新