Skip to content

A Powershell module that moves files from all sub-directories to the parent directory

License

Notifications You must be signed in to change notification settings

trossr32/ps-flatten-folders

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlattenFolders

PowerShell Gallery Version PowerShell Gallery

A Powershell module that moves files from all sub-directories to the parent directory.

Available in the Powershell Gallery

Description

Moves files from all sub-directories to the parent directory. If files with duplicate names are found then their file name will have a guid appended to make them unique.

Supports WhatIf. If supplied this will output a formatted table of the from and to file locations that will result from running the cmdlet.

Can be run against:

  • a single directory
  • a collection of directories piped into the module.

Installation (from the Powershell Gallery)

Install-Module FlattenFolders
Import-Module FlattenFolders

Parameters

-Directory (alias -D)

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.

-Directories

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.

-WhatIf

Optional. If supplied this will output a formatted table of the from and to file locations that will result from running the cmdlet.

-DeleteSubDirectories (alias -DS)

Optional. If supplied all sub-directories will be deleted once all files have been moved.

Example

Given the following directory structure:

📦flatten me
┣ 📂parent a
┃ ┣ 📂sub a
┃ ┃ ┣ 📜a a New Text Document - Copy (2).txt
┃ ┃ ┣ 📜a a New Text Document - Copy.txt
┃ ┃ ┗ 📜a a New Text Document.txt
┃ ┣ 📂sub b
┃ ┃ ┣ 📜a b New Text Document - Copy (2).txt
┃ ┃ ┣ 📜a b New Text Document - Copy.txt
┃ ┃ ┗ 📜a b New Text Document.txt
┃ ┣ 📂sub c
┃ ┃ ┣ 📜a c New Text Document - Copy (2).txt
┃ ┃ ┣ 📜a c New Text Document - Copy.txt
┃ ┃ ┗ 📜a c New Text Document.txt
┃ ┗ 📂sub d
┃ ┃ ┣ 📜a c New Text Document - Copy.txt
┃ ┃ ┣ 📜New Text Document - Copy (2).txt
┃ ┃ ┗ 📜New Text Document.txt
┣ 📂parent b
┃ ┣ 📂sub a
┃ ┃ ┣ 📜New Text Document - Copy (2).txt
┃ ┃ ┣ 📜New Text Document - Copy.txt
┃ ┃ ┗ 📜New Text Document.txt
┃ ┣ 📂sub b
┃ ┃ ┣ 📜b New Text Document - Copy (2).txt
┃ ┃ ┣ 📜b New Text Document - Copy.txt
┃ ┃ ┗ 📜b New Text Document.txt
┃ ┣ 📂sub c
┃ ┃ ┣ 📜c New Text Document - Copy (2).txt
┃ ┃ ┣ 📜c New Text Document - Copy.txt
┃ ┃ ┗ 📜c New Text Document.txt
┃ ┗ 📂sub d
┃ ┃ ┣ 📜c New Text Document - Copy.txt
┃ ┃ ┣ 📜New Text Document - Copy (2).txt
┃ ┃ ┗ 📜New Text Document.txt
┗ 📂parent c

Running the following command...

PS C:\>@("C:\temp\flatten me\parent a","C:\temp\flatten me\parent b","C:\temp\flatten me\parent c") | Invoke-FlattenFolders -DeleteSubDirectories

...will move all files from each parent directory's sub-directories into the parent directory and then delete the sub-directories. Files with duplicate names will have a Guid appended to their file names. The result will look like this:

📦flatten me
┣ 📂parent a
┃ ┣ 📜a a New Text Document - Copy (2).txt
┃ ┣ 📜a a New Text Document - Copy.txt
┃ ┣ 📜a a New Text Document.txt
┃ ┣ 📜a b New Text Document - Copy (2).txt
┃ ┣ 📜a b New Text Document - Copy.txt
┃ ┣ 📜a b New Text Document.txt
┃ ┣ 📜a c New Text Document - Copy (2).txt
┃ ┣ 📜a c New Text Document - Copy_58888d2c-b089-472b-b166-742701456252.txt
┃ ┣ 📜a c New Text Document - Copy_59612b2e-d42b-474d-b522-1ffdc4e302fb.txt
┃ ┣ 📜a c New Text Document.txt
┃ ┣ 📜New Text Document - Copy (2).txt
┃ ┗ 📜New Text Document.txt
┣ 📂parent b
┃ ┣ 📜b New Text Document - Copy (2).txt
┃ ┣ 📜b New Text Document - Copy.txt
┃ ┣ 📜b New Text Document.txt
┃ ┣ 📜c New Text Document - Copy (2).txt
┃ ┣ 📜c New Text Document - Copy_1e57ea51-bc54-4f44-a1db-98257b4e839b.txt
┃ ┣ 📜c New Text Document - Copy_eb5b533c-19b5-4898-9c60-5edc6e6d7ceb.txt
┃ ┣ 📜c New Text Document.txt
┃ ┣ 📜New Text Document - Copy (2)_2a39376b-7b8b-4087-bfc4-7c0f25cfc96e.txt
┃ ┣ 📜New Text Document - Copy (2)_6ab8a5a8-a7b7-4e2b-8eb2-c6e64d7458ea.txt
┃ ┣ 📜New Text Document - Copy.txt
┃ ┣ 📜New Text Document_0495b454-56d7-41a6-9f6c-f4ce39a35c3a.txt
┃ ┗ 📜New Text Document_79c2dd84-b1bf-4660-ba10-3229848b867f.txt
┗ 📂parent c

Further examples

All files in all sub-directories in the current location (C:) will be moved to the current location (C:):

PS C:\> Invoke-FlattenFolders

Displays an output table to terminal detailing that all files in all sub-directories in C:\Videos\ would be moved to C:\Videos:

PS C:\> Invoke-FlattenFolders -Directory "C:\Videos" -WhatIf

All files in all sub-directories in C:\Videos\ will be moved to C:\Videos\ and all sub-directories will be deleted once the files have been moved:

PS C:\> Invoke-FlattenFolders -Directory "C:\Videos" -DeleteSubDirectories

All files in all sub-directories in the piped array of directories (C:\Videos\ and C:\Music) will be moved to their respective parents:

PS C:\> "C:\Videos\","C:\Music\" | Invoke-FlattenFolders

Building the module and importing locally

Build the .NET core solution

dotnet build [Github clone/download directory]\ps-flatten-folders\src\PsFlattenFoldersCmdlet.sln

Copy the built files to your Powershell modules directory

Remove any existing installation in this directory, create a new module directory and copy all the built files.

Remove-Item "C:\Users\[User]\Documents\PowerShell\Modules\FlattenFolders" -Recurse -Force -ErrorAction SilentlyContinue
New-Item -Path 'C:\Users\[User]\Documents\PowerShell\Modules\FlattenFolders' -ItemType Directory
Get-ChildItem -Path "[Github clone/download directory]\ps-flatten-folders\src\PsFlattenFoldersCmdlet\bin\Debug\netcoreapp3.1\" | Copy-Item -Destination "C:\Users\[User]\Documents\PowerShell\Modules\FlattenFolders" -Recurse

Import the module to your session

Import-Module "C:\Users\[User]\Documents\PowerShell\Modules\FlattenFolders\FlattenFolders.dll"

Notes

Initially this module was written in native Powershell but has since been upgraded to a .NET 8 Cmdlet. I've archived the Powershell version in case anyone is interested in viewing the differences between the implementations.

Contribute

Please raise an issue if you find a bug or want to request a new feature, or create a pull request to contribute.

Buy Me a Coffee at ko-fi.com