Skip to content

Instantly share code, notes, and snippets.

@rytsikau
Last active November 8, 2020 07:25
Show Gist options
  • Save rytsikau/869e2d417c35ba4c14b83181e36fe404 to your computer and use it in GitHub Desktop.
Save rytsikau/869e2d417c35ba4c14b83181e36fe404 to your computer and use it in GitHub Desktop.
Renames folders with the last write time of their most recent files
'-------------------------------------------------------------------------------------------------'
' renameFoldersWithItsLastEditDates (2020-11-08) '
' Renames folders with the last write time of their most recent files '
' [directoryName] -> [directoryName]_[YYYYMMDD-HHmmss] '
'-------------------------------------------------------------------------------------------------'
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
$erroractionpreference = "SilentlyContinue"; Remove-Variable *; $erroractionpreference = "Continue"
$pathWithFoldersToRename = "c:\FoldersToRename\"
Write-Host "Renaming folders in $pathWithFoldersToRename..."
$cnt = 0
foreach ($item in (Get-ChildItem $pathWithFoldersToRename -Directory -Force))
{
$dtRecent = (Get-ChildItem $item.FullName -Recurse -File -Force `
| Sort LastWriteTime | Select -Last 1).LastWriteTime
$newName = "$($dtRecent.ToString('yyyyMMdd-HHmmss'))_$($item.Name)"
Rename-Item $item.FullName -NewName $newName
$cnt++
}
Write-Host "Ready! $cnt folders renamed"
'-------------------------------------------------------------------------------------------------'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment