This is a modified version of FlattenFolders by Rob Green. I've simply added an additional method of renaming duplicate files.
Install-Module FMFlattenFolders
Import-Module FMFlattenFolders
Moves files from all sub-directories to the parent directory. If files with duplicate names are found, they will have either a GUID or a numeric index appended to the file name depending on the passed value of -RenameMethod
.
The cmdlet also Supports -WhatIf
. If supplied this will output a formatted table of the final locations of all files.
The below will completely flatten C:\Music
, copying all files in all subdirectories recursively to the root.
Invoke-FlattenFolders -Directory "C:\Music"
The below will completely flatten D:\Icons\SVG
, copying all files in all subdirectories recursively to the root, and on completion delete all empty subdirectories after the move.
Invoke-FlattenFolders -Directory "D:\Icons\SVG" -DeleteSubDirectories
You can also pipe multiple directories to flatten in an array:
@("C:\MyVideos", "C:\ConvertedAudio", "D:\SomeOther\Directory") |
Invoke-FlattenFolders -DeleteSubDirectories
-RenameMethod
determines how duplicate files are handled during the directory flattening operation.
-RenameMethod Index
will append a numeric index to the end of duplicate files.
-RenameMethod GUID
will append a random GUID to the end of duplicate files.
Invoke-FlattenFolders -Directory "D:\Icons\SVG" -DeleteSubDirectories -RenameMethod Index
Optional. The parent directory where files from all sub-directories will be moved. If neither this nor the Directories parameter are set then the current location will be used.
Optional. A collection of parent directories where files from all sub-directories will be moved. If neither this nor the Directory parameter are set then the current location will be used.
Optional. If supplied this will output a formatted table of the from and to file locations that will result from running the cmdlet.
Optional. If supplied all sub-directories will be deleted once all files have been moved.
Optional. The strategy for renaming duplicate files. Valid values are "Guid" or "Index". Defaults to Index.