Skip to content

Instantly share code, notes, and snippets.

@iamkarlson
Created May 21, 2017 07:11
Show Gist options
  • Select an option

  • Save iamkarlson/f1c1643f83460344ed05d45aedab06d5 to your computer and use it in GitHub Desktop.

Select an option

Save iamkarlson/f1c1643f83460344ed05d45aedab06d5 to your computer and use it in GitHub Desktop.
arrange files by year/month folder
function Group-ByDate(){
[Cmdletbinding(SupportsShouldProcess=$TRUE)]
param
(
[parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0)]
[String[]]$Folder
)
Write-Output "Arranging files in $folder"
$exist=Test-Path $folder;
if($exist){
ls -Path $folder | % {
write-output "Perform move operation to "$_.FullName
$file = $_.FullName
$date = Get-Date ($_.LastWriteTime)
$month = $date.ToString('MM')
$year = $date.ToString('yyyy')
$newFolder = [system.io.path]::Combine($folder,"$year","$month")
if(!(test-path $newFolder)){
Write-output "Creating "$newFolder
new-item -type Directory -path $newFolder
}
Write-Output "Moving "$file" to"$newFolder
move-item $file -Destination $newFolder
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment