PoshCode Archive  Artifact [271a638f5a]

Artifact 271a638f5a1dd362280b9fd7767f36bd46e89320ac7c131fa4f6aeb26ec5c91e:

  • File Copy-File-Safely.ps1 — part of check-in [ecf27634e0] at 2018-06-10 13:02:58 on branch trunk — Recursively copies all files and folders in $source folder to $destination folder, but with .copy inserted before the extension if the file already exists (user: Joel Bennett size: 1145)

# 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
  }
}
}