Skip to content

Instantly share code, notes, and snippets.

@daephx
Last active November 12, 2022 19:50
Show Gist options
  • Save daephx/68612eee5814f5cf5635881fa5922951 to your computer and use it in GitHub Desktop.
Save daephx/68612eee5814f5cf5635881fa5922951 to your computer and use it in GitHub Desktop.
[Split-Filetypes] Create Directories and move files for each files extension. #PowerShell
<#
.SYNOPSIS
Create Directories and move files for each files extension.
#>
param([string]$Directory = $PWD)
function Move-File($x, $foldername) {
If (!(Test-Path $Directory\$foldername)) {
New-Item -ItemType Directory -Path $Directory -Name $foldername -ErrorAction Ignore | Out-Null
}
elseif (!(Test-Path $Directory\$foldername\$x.name)) {
Move-Item -Path "$($x.FullName)" "$($Directory)\$($foldername)" # -ErrorAction SilentlyContinue
$Message = ("Files: '$($x.FullName)' -> '$($Directory)\$($foldername)\$($x.name)'")
Write-Output $Message
}
}
$files = Get-ChildItem -Path $Directory -File
Foreach ($x in $files) { Move-File $x $x.Extension.TrimStart('.').ToLower() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment