How to rename files by date with PowerShell including characters between it

Luis Matas Royo 0 Reputation points
2023-05-28T10:41:00.9566667+00:00

Good afternoon: First of all, note that I'm very new to this, so maybe I don't express myself as I should, I hope you understand me. I am trying to bulk rename a folder of images and videos based on the date they were taken. For now, this is the "expression" I'm using:

Get-ChildItem | Rename-Item -NewName {$.LastWriteTime.ToString("yyyy-MM-dd hh.mm.ss") + ($.Extension)}

With it, I get any file to be renamed in a way similar to the following:

2023-05-27_18.19.15

However, I would like to use te EXIF date of te JPGs instead of last write time, and the name to be the following:

2023-05-27_18h19m15

Thank you for your help.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-05-28T20:05:38.7433333+00:00

    Getting the EXIF data from a file isn't that easy. For something simple, start with this code to find the exif property "Date Taken" (it's probably #12). Then modify the code a bit to just get that single property and format it the way you want it to look.

    function Get-FileMetaData{
        param(
            [Parameter(Mandatory=$True)]
            [string]$File
        )
        if(!(Test-Path -Path $File)){
            throw "File does not exist: $File"
            Exit 1
        }
        $tmp = Get-ChildItem $File
        $pathname = $tmp.DirectoryName
        $filename = $tmp.Name
        try{
            $shellobj = New-Object -ComObject Shell.Application
            $folderobj = $shellobj.namespace($pathname)
            $fileobj = $folderobj.parsename($filename)
            for($i=0; $i -le 321; $i++){
                $name = $folderobj.getDetailsOf($null, $i)
                if($name){
                    $value = $folderobj.getDetailsOf($fileobj, $i)
                    if($value){
                        "{0}`t{1} {2} {3}" -f $i,$name,('.' * 30),$value
                    }
                }
            }
        }finally{
            if($shellobj){
                [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$shellobj) | out-null
            }
        }
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.