Last active
November 8, 2020 07:25
-
-
Save rytsikau/869e2d417c35ba4c14b83181e36fe404 to your computer and use it in GitHub Desktop.
Renames folders with the last write time of their most recent files
This file contains 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
'-------------------------------------------------------------------------------------------------' | |
' 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