Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/trossr32/ps-flatten-folders
- Owner: trossr32
- License: mit
- Created: 2020-06-19T19:29:31.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-01-14T17:26:59.000Z (12 months ago)
- Last Synced: 2024-01-14T22:28:09.082Z (12 months ago)
- Topics: cmdlet, directories, net8, powershell, powershell-module, powershell-modules
- Language: C#
- Homepage: https://powershellgallery.com/packages/FlattenFolders
- Size: 88.9 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 cRunning 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 examplesAll 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.