I have hundreds of JPG files in a folder. I want to rename each file so that the file name is replaced with "Modified Date/Time" of that file, namely DD.MM.RRRR.HH.MM.jpg. For example,

Before    After  

001.jpg   11.01.2011.16.58.jpg  
002.jpg   12.01.2011.09.32.jpg  
003.jpg   14.01.2011.12.41.jpg  
...       ...

Since colon (:) cannot be used in file names, the colon between HH and MM must be replaced with a period.

I don't want to use a 3rd party tool. Can you provide me with the code to achieve this in Powershell or command line?


Try this in Powershell:

Get-ChildItem *.jpg | Rename-Item -newname {$_.LastWriteTime.toString("dd.MM.yyyy.HH.mm") + ".jpg"}

I hope you guys don't mind my feedback here.

'Rename-Item' gave me this error: Rename-Item : Cannot create a file when that file already exists.

So I've added to Siim K's solution as follows and ran this within 'Windows Powershell ISE':

Get-ChildItem *.jpg |ForEach-Object { $NewName = $.LastWriteTime.toString("yyyy.MM.dd.HH.mm.ss.ss")+($script:i++) + ".jpg" $Destination = Join-Path -Path $.Directory.FullName -ChildPath $NewName Move-Item -Path $_.FullName -Destination $Destination -Force }

I found the 'yyyy.MM.dd.mm.ss.ss' order listed my images better and I recognized than none of my image count was reduced (accidentally purged) within the folder as well. I hope this is helpful to other 'superusers' (beginner or experienced).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.