Powershell - rename file using Date Taken attribute

非 Y 不嫁゛ 提交于 2019-12-11 04:31:39

问题


I have a stack load of images and videos on my Samsung phone. I copied these images to a USB then onto my PC.

I want to use Powershell to rename these files based on their Date Taken attribute.

Format required = yyyy-MM-dd HH.mm.ss ddd

I have been using a Powershell script (see below) that does this beautifully using the Date Modified attribute, but the copy above somehow changed the Date Modified value on me (WTH!), so I can't use that now (as its not accurate).

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

In summary - is there a way to change the file name based on the Date Taken file attribute? Suggestions I have seen online require use of the .NET System.Drawing.dll and convoluted code (I'm sure it works, but damn its ugly).

GG


回答1:


Please checkout Set-PhotographNameAsDateTimeTaken Powershell module. It extract date and time from the picture and change name of the picture to it.

It allows to use -Recurse -Replace and -Verbose parameter. By default it will create reuslt folder at the same level as your working dir.

If you need change the format of the target names the code can be found here.




回答2:


I 'glued' together a bunch of other answers to make a bulk script. Credit to those, but Chrome crashed and I lost those other webpages on Stack. This works on photo files only and will rename all files to YYYYMMDD_HHMMSS.jpg format.

Here it is:

$nocomment = [reflection.assembly]::LoadWithPartialName("System.Drawing")
get-childitem *.jpg | foreach {
    $pic = New-Object System.Drawing.Bitmap($_.Name)
    $bitearr = $pic.GetPropertyItem(36867).Value
    $string = [System.Text.Encoding]::ASCII.GetString($bitearr)
    $date = [datetime]::ParseExact($string,"yyyy:MM:dd HH:mm:ss`0",$Null)
    [string] $newfilename = get-date $date -format yyyyMd_HHmmss
    $newfilename += ".jpg"
    $pic.Dispose()
    rename-item $_ $newfilename -Force
    $newfilename
}



回答3:


In order to avoid this error:

New-Object : Cannot find type [System.Drawing.Bitmap]: verify that the assembly containing this type is
loaded.
...

Make sure the required assembly is loaded before executing the code above:

add-type -AssemblyName System.Drawing


来源:https://stackoverflow.com/questions/38810203/powershell-rename-file-using-date-taken-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!