Last active
September 7, 2020 23:35
-
-
Save joymon/4ac530b15de38978d21176d47d86e720 to your computer and use it in GitHub Desktop.
Media organizer
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
# Target Filder where files should be moved to. The script will automatically create a folder for the year and month. | |
$sourcePath = 'd:\Media' | |
$targetPath = 'd:\Media' | |
# Get the files which should be moved, without folders | |
$files = Get-ChildItem $sourcePath | where {!$_.PsIsContainer} | |
foreach ($file in $files) | |
{ | |
# Get year and Month of the file. Used LastWriteTime. | |
$date = "{0:00}" -f $file.LastWriteTime.Day | |
$month = "{0:00}" -f $file.LastWriteTime.Month | |
$year = $file.LastWriteTime.Year.ToString() | |
# Set Directory Path | |
$Directory = $targetPath + "\" + $year + "\" + $month + "\" + $date | |
# Create directory if it doesn't exsist | |
if (!(Test-Path $Directory)) | |
{ | |
New-Item $Directory -type directory | |
} | |
# Out FileName, year and month | |
$DestinationFilePath = Join-Path -Path $Directory -ChildPath $file.Name | |
# Move File to new location if not exists | |
$counter = 0 | |
while(Test-Path -Path $DestinationFilePath) | |
{ | |
$DestinationFilePath = Join-Path $Directory ($file.BaseName+ "_renamed$counter" + $file.Extension) | |
$counter = $counter+1 | |
} | |
$DestinationFilePath | |
$file | Move-Item -Destination $DestinationFilePath | |
} |
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
# Move files from inside folders to root in case they are not following convention | |
$sourcePath = 'd:\Media' | |
$rootPath = 'd:\Media\' | |
$files = Get-ChildItem $sourcePath -Recurse ` | |
| where {!$_.PsIsContainer} ` | |
| where {$_.DirectoryName -notmatch '\d\d\d\d-\d\d-\d\d'} ` | |
| where {$_.DirectoryName -notmatch '\d\d\d\d\\\d\d\\\d\d'} | |
$files ` | |
| select {$_.FullName} ` | |
| Move-Item -Destination $rootPath | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment