Created
July 22, 2021 18:53
-
-
Save ChadDevOps/22c1f8102dfb856e073bbf065d39183a to your computer and use it in GitHub Desktop.
Copy all files to new folder in parent directory, rename if same
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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