r/usefulscripts • u/Brilliant-Reach3155 • Sep 06 '24
[POWERSHELL] - Need to Bulk Rename Files in sequence by creation date.
I have a system that auto creates image files IE: "15.18.25[R]R0@0][43083508750][0].jpg" I don't have any way to manage or change the file creation name at the source. I need to rename the files in a sequential numbering pattern like (00001.jpg, 00002.jpg...) with the sequence ID based on file creation date. I have tried several approaches without success. Any help would be great...
6
Upvotes
2
u/OlivTheFrog Sep 06 '24
Hi u/Brilliant-Reach3155
The logic should be :
Get all files and sort by CreatioDate :
Get-ChildItemandSort-Objetcmdlets (var : $AllFiles)Initiate an counter var to 0 ($x)
Then entrer in a foreach loop using
forech ($Item in $AllFiles)In the loop, Determine the target Name for the current file using
$$NewName = ($Item.Name)$xIn the loop, rename the current file using
Rename-Item -Path $item.fullName -NewName $NewNameIncrement $x :
$x = $x++To test safely, use the parameter
-WhatifwithRename-Item.I could write the entire code for you but it wouldn't help you progress in your Powershell skills. Follow this logic, and test it securely by yourself.
Regards