Last active
September 28, 2023 14:37
-
-
Save DEARaison/3f21ee748bda6693b093443c3e304e08 to your computer and use it in GitHub Desktop.
Group Files By Extension Recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Please note that this script will modify your directory, You must back up your directory by yourself!" | |
#pause | |
$currentLocation = Get-Item -Path "" | |
# Create Grouped By Extension directory | |
$parentOfCurrentLocation = Split-Path -Parent $currentLocation | |
$groupedByExtensionLocation = Join-Path -Path $parentOfCurrentLocation -ChildPath "Grouped By Extension" | |
New-Item -Force -ItemType Directory $groupedByExtensionLocation | |
Get-ChildItem -Path $currentLocation -File -Recurse -Force | ForEach-Object { | |
# Create current file extension group directory | |
$extensionGroupLocation = Join-Path -Path $groupedByExtensionLocation -ChildPath $_.Extension | |
New-Item -Force -ItemType Directory $extensionGroupLocation | |
$fileNewLocation = Join-Path -Path $extensionGroupLocation -ChildPath $_.Name | |
If (Test-Path -PathType Leaf -Path $fileNewLocation) { | |
Remove-Item -Force -Path $fileNewLocation | |
} | |
Move-Item -Path $_.FullName -Destination $fileNewLocation | |
} # End ForEach-Object. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment