Skip to content

Instantly share code, notes, and snippets.

@Ghostlyr
Created August 16, 2018 16:30
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 Ghostlyr/80d44b89ee66a6c496cecefa2f990fa8 to your computer and use it in GitHub Desktop.
Save Ghostlyr/80d44b89ee66a6c496cecefa2f990fa8 to your computer and use it in GitHub Desktop.
PowerShell - Sort files by date
# 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