Skip to content

Instantly share code, notes, and snippets.

@SteveH3032
Created November 25, 2018 03:16
Show Gist options
  • Save SteveH3032/9d8a815b4edbcae9f82a4d916ef056d5 to your computer and use it in GitHub Desktop.
Save SteveH3032/9d8a815b4edbcae9f82a4d916ef056d5 to your computer and use it in GitHub Desktop.
PowerShell Rename and Organize Media Files
cd "C:\path\to\media"
# Rename Media based on the 'CreationTime'
Get-ChildItem *.jpg -Recurse | Rename-Item -newname {$_.CreationTime.toString("yyyyMMdd_HHmmss") + $_.Extension} -ErrorAction SilentlyContinue
Get-ChildItem *.jpeg -Recurse | Rename-Item -newname {$_.CreationTime.toString("yyyyMMdd_HHmmss") + $_.Extension} -ErrorAction SilentlyContinue
Get-ChildItem *.png -Recurse | Rename-Item -newname {$_.CreationTime.toString("yyyyMMdd_HHmmss") + $_.Extension} -ErrorAction SilentlyContinue
Get-ChildItem *.mp4 -Recurse | Rename-Item -newname {$_.CreationTime.toString("yyyyMMdd_HHmmss") + $_.Extension} -ErrorAction SilentlyContinue
Get-ChildItem *.jpg, *.jpeg, *.png, *.mp4 -Recurse -ErrorAction SilentlyContinue | ForEach-Object -process {
# Set Output Folder Based on File Type
$DestinationFolder = If ($_.Extension -eq ".mp4") {"S:\Video"} Else {"S:\Photo"}
# Set Output Path w/File Name
$OutFile = Join-Path -Path $DestinationFolder -ChildPath $_.Name
# Check if File Exists in New Directory - By Name & File Size
If ((Test-Path -Path $OutFile) -and ((Get-Item $_).Length -eq (Get-Item $OutFile).Length))
{
Write-Host "Deleted File: '$_.Name' : Identical 'Name' & 'Size'." + (Get-Item $_).Length = (Get-Item $OutFile).Length
#Remove-Item $_
}
# Check if File Exists in New Directory - By Name Only
ElseIf (Test-Path -Path $OutFile){
$OutFile = Join-Path $DestinationFolder ($_.BaseName + "(1)" + $_.Extension)
$_ | Move-Item -Destination $OutFile -ErrorAction SilentlyContinue
Write-Host "Moved file: '$_ (1)' --> $OutFile"
}
Else {
$_ | Move-Item -Destination $OutFile -ErrorAction SilentlyContinue
Write-Host "Moved file: '$_' --> $OutFile"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment