Advertisement
LarsFosdal

PS - ReplaceFiles script

Nov 22nd, 2016
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. Replacefiles copies from one directory to the other, optionally only replacing files that exist in the target directory
  4. .DESCRIPTION
  5. Replacefiles copies from one directory to the other, optionally only replacing files that exist in the target directory
  6. .PARAMETER SourceDirectory
  7. Location of new files \\SourceLocation
  8. .PARAMETER TargetDirectory
  9. Location of old files \\TargetLocation
  10. .PARAMETER Filenames
  11. Name of files to copy -Filenames fileone.ext, filetwo.ext, file3.ext
  12. .PARAMETER AlwaysCopy
  13. Replacefiles will also copy files that are not present in the target directory
  14. .EXAMPLE
  15. ReplaceFiles -source C:\Build\Executables -target \\SomewhereElse -filenames thisfile.exe, somefile.txt, thatfile.exe -AlwaysCopy
  16. #>
  17. [CmdletBinding()]
  18. param (
  19. [Parameter(Mandatory=$True)]
  20. [Alias('source')]
  21. [String]$SourceDirectory,
  22. [Parameter(Mandatory=$True)]
  23. [Alias('target')]
  24. [String]$TargetDirectory,
  25. [Parameter(Mandatory=$True)]
  26. [String[]]$Filenames,
  27. [switch]$AlwaysCopy
  28. )
  29.  
  30. if (test-path $SourceDirectory)
  31. {
  32.   if (test-path $TargetDirectory)
  33.   {
  34.     Write-Verbose "Replace files in $TargetDirectory from $SourceDirectory"
  35.     if ($alwayscopy)
  36.     {
  37.         Write-Verbose "and add files that are not already present."
  38.     }
  39.  
  40.     foreach($file in $Filenames)
  41.     {
  42.         Write-Verbose $file
  43.         $new = Join-Path $SourceDirectory $file
  44.         $old = Join-Path $TargetDirectory $file
  45.         $newtmp = $old + '.new'
  46.         $tmp = $old + '.tmp'
  47.         $tmpfile = $file + '.tmp'
  48.        
  49.         $cpy = $AlwaysCopy
  50.         if (Test-Path $old)
  51.         {
  52.             $cpy = $True
  53.         }
  54.        
  55.         if (-not (Test-Path $New))
  56.         {
  57.             Write-Host $new not found
  58.             $cpy = $False
  59.         }
  60.  
  61.         if ($cpy)
  62.         {
  63.        
  64.             Write-Host "Copying $file"
  65.  
  66.             if (Test-Path $tmp)
  67.             {
  68.                 Write-Verbose "Deleting $tmp"
  69.                 Remove-Item $tmp -Force -ErrorAction SilentlyContinue
  70.             }
  71.  
  72.             if (Test-Path $newtmp)
  73.             {
  74.                 Write-Verbose "Deleting $newtmp"
  75.                 Remove-Item $newtmp -Force -ErrorAction SilentlyContinue
  76.             }
  77.  
  78.             Write-Verbose "Copying $new to $newtmp"
  79.             Copy-Item $new -Destination $newtmp -Force
  80.  
  81.             if (Test-Path $old)
  82.             {
  83.                 Write-Verbose "Renaming $file to $tmpfile"
  84.                 Rename-Item $old -NewName $tmpfile -Force
  85.             }
  86.  
  87.             Write-Verbose "Renaming $newtmp to $file"
  88.             Rename-Item $newtmp -NewName $file -Force
  89.  
  90.             if (Test-Path $tmp)
  91.             {
  92.                 Write-Verbose "Deleting $tmp"
  93.                 Remove-Item $tmp -Force -ErrorAction SilentlyContinue
  94.             }
  95.         }
  96.         else {
  97.             Write-Verbose "Skipped $file"
  98.         }
  99.     }
  100.     Write-Verbose "ReplaceFiles completed"
  101.   }
  102.   else {
  103.     Write-Error "Target directory $TargetDirectory does not exists"
  104.   }
  105. } else {
  106.   Write-Error "Source directory $SourceDirectory does not exists"
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement