3 ms·
A while ago I was trying to copy a bunch of files and couldn't believe the powershell abominations I found: https://techblog.dorogin.com/powershell-how-to-recur
by flukus 8y ago
A while ago I was trying to copy a bunch of files and couldn't believe the powershell abominations I found: https://techblog.dorogin.com/powershell-how-to-recursively-copy-a-folder-structure-excluding-some-child-folders-and-files-a1de7e70f1b?gi=be511f23383a https://techblog.dorogin.com/powershell-how-to-recursively-c...
$exclude = @("main.js")
$excludeMatch = @("app")
Get-ChildItem -Path $from -Recurse -Exclude $exclude |
where { $excludeMatch -eq $null -or $_.FullName.Replace($from, "") -notmatch $excludeMatch } |
Copy-Item -Destination {
if ($_.PSIsContainer) {
Join-Path $to $_.Parent.FullName.Substring($from.length)
} else {
Join-Path $to $_.FullName.Substring($from.length)
}
} -Force -Exclude $exclude
All to do:
find sourceFolder \! -path */app/* | xargs cp -t destFolder
Screwed up the formatting a bit in the first example. I'm not positive the second works perfectly but I know which one I'd rather debug.
- h1d 8y agorsync -a --exclude=app/ --exclude=main.js sourceFolder/ destFolder/ Thank you, thank you unix tools.
- AnIdiotOnTheNet 8y agoFirst of all, the two examples you present do not do the same thing. The Powershell version preserves paths and excludes more patterns than your find | xargs example. The right way to do this is to just use robocopy. This example has targeted a very specific deficiency in the way Copy-Item works. I could similarly point out that the following Powershell command would be much more difficult in standard unix tooling: Get-WMIObject -Computer $remote -Class Win32_Product | Where-Object { $_.name -like "7-Zip*" } | select Version Which gets the version(s) of 7-Zip installed on a remote machine.