Skip to content

Instantly share code, notes, and snippets.

@ChadDevOps
Created July 22, 2021 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChadDevOps/22c1f8102dfb856e073bbf065d39183a to your computer and use it in GitHub Desktop.
Save ChadDevOps/22c1f8102dfb856e073bbf065d39183a to your computer and use it in GitHub Desktop.
Copy all files to new folder in parent directory, rename if same
function fcopy ($SourceDir,$DestinationDir)
{
Get-ChildItem $SourceDir -Recurse | Where-Object { $_.PSIsContainer -eq $false } | ForEach-Object ($_) {
$SourceFile = $_.FullName
$DestinationFile = $DestinationDir + $_
write-host "------------------------------------"
write-host "Source: $SourceFile"
write-host "Dest : $DestinationFile"
if (Test-Path "$DestinationFile"){
if( (Get-FileHash "$SourceFile").Hash -ne (Get-FileHash "$DestinationFile").Hash ) {
$i = 0
while (Test-Path "$DestinationFile") {
$i += 1
$DestinationFile = $DestinationDir + $_.basename + '-' + $i + $_.extension
if ((Get-FileHash "$SourceFile").Hash -eq (Get-FileHash "$DestinationFile").Hash ) {
break
}
write-host $i
}
if ((Get-FileHash "$SourceFile").Hash -ne (Get-FileHash "$DestinationFile").Hash ) {
write-host "Rename and copy to $DestinationFile"
Copy-Item -Path "$SourceFile" -Destination "$DestinationFile"
}
}
} else {
write-host "Copying to $DestinationFile"
Copy-Item -Path "$SourceFile" -Destination "$DestinationFile"
}
}
}
$currentLoc = Get-Location
$ShortPath = (New-Object -ComObject Scripting.FileSystemObject).GetFolder($currentLoc).ShortPath
$parentFolder = split-path $ShortPath
$currentFolder = Split-Path -Path (Get-Location) -Leaf
$copyTo = $parentFolder + '\' + $currentFolder + " - ALL\"
$temp = New-Item -ItemType Directory -Force -Path "$copyTo"
fcopy -SourceDir $ShortPath -DestinationDir "$copyTo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment