Last active
November 12, 2022 19:50
-
-
Save daephx/68612eee5814f5cf5635881fa5922951 to your computer and use it in GitHub Desktop.
[Split-Filetypes] Create Directories and move files for each files extension. #PowerShell
This file contains hidden or 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
<# | |
.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