- $filter = '*.dll', '*.pdb'
- $copied = 0; $skipped = 0; $failed = 0
- $sourceFiles = $filter | % { Get-ChildItem $sourceDir -Filter $_ -Recurse } `
- | sort @{expression={$_.FullName.Length}} -Descending `
- | group Name -AsHashTable
- $targetFiles = $filter | % { Get-ChildItem $targetDir -Filter $_ -Recurse } `
- | ? { $sourceFiles.ContainsKey($_.Name) } `
- | sort FullName
- $targetFiles | % {
- $target = $_
- $sourceFiles[$target.Name] | % {
- $source = $_
- if ($target.FullName.EndsWith($source.FullName.Substring($sourceDir.Length),
- 'InvariantCultureIgnoreCase')) {
- if ($target.LastWriteTime.AddSeconds(2) -lt $source.LastWriteTime) {
- Write-Host $target.FullName
- try {
- Copy-Item $source.FullName $target.FullName -Force
- $copied++
- }
- catch {
- Write-Error $_.Message
- $failed++
- }
- }
- else {
- $skipped++
- }
- return
- }
- }
- }
- Write-Host "$copied copied, $skipped skipped, $failed failed in $($watch.Elapsed)"
Sunday, November 24, 2013
Recursively search and replace files from one folder into another
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment