Created
May 21, 2017 07:11
-
-
Save iamkarlson/f1c1643f83460344ed05d45aedab06d5 to your computer and use it in GitHub Desktop.
arrange files by year/month folder
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
| 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