Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/trossr32/ps-flatten-folders

A Powershell module that moves files from all sub-directories to the parent directory
https://github.com/trossr32/ps-flatten-folders

cmdlet directories net8 powershell powershell-module powershell-modules

Last synced: 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

# FlattenFolders

[![PowerShell Gallery Version](https://img.shields.io/powershellgallery/v/FlattenFolders?label=FlattenFolders&logo=powershell&style=plastic)](https://www.powershellgallery.com/packages/FlattenFolders)
[![PowerShell Gallery](https://img.shields.io/powershellgallery/dt/FlattenFolders?style=plastic)](https://www.powershellgallery.com/packages/FlattenFolders)

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

Available in the [Powershell Gallery](https://www.powershellgallery.com/packages/FlattenFolders)

## 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)

```powershell
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...

```powershell
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:\):

```powershell
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\:

```powershell
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:

```powershell
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:

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

## Building the module and importing locally

### Build the .NET core solution

```powershell
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.

```powershell
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

```powershell
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.

- The .NET core version of the module is in the [src](https://github.com/trossr32/ps-flatten-folders/tree/master/src) directory.
- The Native Powershell version is in the [archived native PS version](https://github.com/trossr32/ps-flatten-folders/tree/master/archived%20native%20PS%20version) directory.

## 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