Skip to content

Instantly share code, notes, and snippets.

@iqwirty
Created April 9, 2014 11:56
Show Gist options
  • Save iqwirty/10260521 to your computer and use it in GitHub Desktop.
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
# 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