Created
April 9, 2014 11:56
-
-
Save iqwirty/10260521 to your computer and use it in GitHub Desktop.
Flattens a folder tree by moving all files to a new destination folder
This file contains 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
# Flatten a folder tree by moving contents of the source path | |
# recursively to the destination path. | |
# | |
# If there is a collision in the destination due to two files | |
# having the same name, then a timestamp is appended to the | |
# base name of the file being moved to create a new unique name. | |
Clear-Host | |
$sourceFolderFullPath = "c:\source" | |
$destinationFolderFullPath = "c:\destination" | |
Get-ChildItem -File -Recurse -Path $sourceFolderFullPath -Filter *.* | ` | |
ForEach-Object ` | |
{ | |
$newName = "$destinationFolderFullPath\$_" | |
If (Test-Path -Path $newName) | |
{ | |
$timestamp = Get-Date -Format o | foreach {$_ -Replace ":", "."} | |
$newName = "$destinationFolderFullPath\$($_.BaseName)_$timestamp$($_.Extension)" | |
} | |
"Moving $($_.FullName) to $newName" | |
Move-Item -Path $_.FullName -Destination $newName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment