# encoding: ascii
# api: powershell
# title: Copy-File (Safely)
# description: Recursively copies all files and folders in $source folder to $destination folder, but with .copy inserted before the extension if the file already exists
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Copy-File
# x-poshcode-id: 1931
# x-archived: 2015-07-02T03:25:13
# x-published: 2010-06-23T14:25:00
#
#
function Copy-File {
#.Synopsis
# Copies all files and folders in $source folder to $destination folder, but with .copy inserted before the extension if the file already exists
param($source,$destination)

# create destination if it's not there ...
mkdir $destination -force -erroraction SilentlyContinue

foreach($original in ls $source -recurse) { 
  $result = $original.FullName.Replace($source,$destination)
  while(test-path $result -type leaf){ $result = [IO.Path]::ChangeExtension($result,"copy$([IO.Path]::GetExtension($result))") }

  if($original.PSIsContainer) { 
    mkdir $result -ErrorAction SilentlyContinue
  } else {
    copy $original.FullName -destination $result
  }
}
}
