Skip to content

Instantly share code, notes, and snippets.

@DEARaison
Last active September 28, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DEARaison/3f21ee748bda6693b093443c3e304e08 to your computer and use it in GitHub Desktop.
Save DEARaison/3f21ee748bda6693b093443c3e304e08 to your computer and use it in GitHub Desktop.
Group Files By Extension Recursively
"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