Created
August 16, 2018 16:30
-
-
Save Ghostlyr/80d44b89ee66a6c496cecefa2f990fa8 to your computer and use it in GitHub Desktop.
PowerShell - Sort files by date
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
# Move files in selected directory to new folders named with file creation date | |
# Author: GhostlyR (https://github.com/Ghostlyr) | |
param ( | |
[string] $dir, | |
[string] $format = "yyyy.MM.dd", | |
[string] $exclude = "sort-by-date.ps1" | |
) | |
if (!$dir) { | |
Write-Host "Wrong directory argument!" | |
} | |
if (!([string] $format -as [DateTime])) { | |
Write-Host "Wrong datetime format argument!" | |
} | |
Get-ChildItem $dir -exclude $exclude | % { | |
$file = $_.FullName | |
$date = Get-Date ($_.CreationTime) | |
$new_filename = $date.ToString($format) | |
New-Item -Type Directory -Force -Path "$dir\$new_filename" | |
Move-Item $file "$dir\$new_filename" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment