Reputation: 87
I found solutions, but this solutions don't exclude the content of the subdirectories excluded.
I want copy recursively a directory to another location, but excluding the subdirectory "Videos" and its content. The next command finish, but no exclude the subdirectory "Videos" and its content.
# PowerShell version: 4.0
# This command copy recursively the directory "J:\All" to "J:\Users\John\Desktop\my_backup", excluding "J:\All\Videos"
Copy-Item -Recurse -Path ("J:\All" | ? { $_.FullName -NotMatch ("^J:\\All\\Videos") }) "J:\Users\John\Desktop\my_backup"
Thank you!
Upvotes: 1
Views: 945
Reputation: 68273
Try this:
$Source = 'J:\All'
$Destination = 'J:\Users\John\Desktop\my_backup\'
$Exclude = 'J:\All\Videos'
Get-Childitem -Recurse -Path $Source |
Where-Object { $Exclude -notcontains $_.Directory } |
foreach {
$TrimmedPath = ($_.fullname -replace [regex]::escape($Source)).trim('\') |
copy-item -Destination "$($Destination.Trim('\'))\$TrimmedPath"
}
Upvotes: 0
Reputation: 28174
RoboCopy is a better solution here. See https://superuser.com/questions/482112/using-robocopy-and-excluding-multiple-directories
robocopy J:\All J:\Users\John\Desktop\my_backup /MIR /XD j:\all\videos
Upvotes: 3