8.20.2023

Bulk Rename Files to Creation Date

 Bulk Rename Files to Creation Date

PowerShell

$ParsedDate = [datetime]::MinValue;
$Path = "D:\Data\SomePath";
Get-ChildItem -File -Path $Path -Recurse |
    Where-Object { (-not [regex]::IsMatch($_.Name, "^\d{8}_", [System.Text.RegularExpressions.RegexOptions]::IgnoreCase)) -or (-not [datetime]::TryParseExact($_.Name.Substring(0, 8), "yyyyMMdd", [cultureinfo]::CurrentCulture, [System.Globalization.DateTimeStyles]::None, [ref] $ParsedDate)) } |
        ForEach-Object { Rename-Item -Path ($_.FullName) -NewName "$($_.CreationTime.ToString("yyyyMMdd"))_$($_.Name)"; }

This PowerShell script will rename all the files in the directory using the format "YYYYMMDD_". This is optimal as Windows filename sorts by this method. 




No comments:

Post a Comment

Note: Only a member of this blog may post a comment.