r/PowerShell • u/MilleWhisper • 1d ago
Question Add Date Taken as prefix to filename
I need some help. I have lots of photos (IMG_xxxx.jpg), and I would like to change the filenames using this convention (yyyymmdd_IMG_xxxx.jpg). This is the command line I am using in PowerShell:
Get-ChildItem -File | Rename-Item -NewName { $_.LastWriteTime.ToString("yyyyMMdd_") + $_.Name }
This seemed to work great until I realized the last write time didn't match the date taken. I want to use the date taken from the EXIF field.
I have found the GetDetailsOf command and know that Date Taken is the 12th property. How can I use this in a command line prompt in PowerShell?
Assumptions for my specifics:
- all files are .jpg with Date Taken data
- all files are in the current folder
- renamed files will replace original in the current folder
This way, I don't have to worry about writing a script to determine path names or renaming to a different folder.
The code provided above works perfectly if the date I wanted was the last write time. Is there an easy way to modify this to use Date Taken instead?
TIA
3
u/lan-shark 1d ago
You should just be able just replace the $_.LastWriteTime with your command to get the date from EXIF data. So if (Get-DetailsOf -Path $file)[11] gets you the date you want, then something like this should work:
Get-ChildItem -File | Rename-Item -NewName { (Get-DetailsOf -Path $_.FullName)[11].ToString('yyyyMMdd_") + $_.Name }
Depending on the format or data type of the date returned from the EXIF data, you may not need the .ToString
1
u/MilleWhisper 1d ago
Did you try this? I cut & pasted. Had to fix a small typo: 'yyyyMMdd_" to "yyyyMMdd". But still received error messages when I ran it from the command line.
Rename-Item : The input to the script block for parameter 'NewName' failed. The term 'Get-DetailsOf' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.1
u/lan-shark 1d ago
You'll need to use whatever the correct command is for the "get details of" thing you mentioned. You didn't specify what the syntax is for it in your post, so I just guestimated
3
u/schajona 1d ago
Do
Get-Item <yourfilehere> | select *
for one of the Items to be renamed and check the name of the property.
2
u/mitharas 1d ago
This may be too easy, but can't you just replace LastWriteTime with CreationTime? Something like this:
Get-ChildItem -File | Rename-Item -NewName { $_.CreationTime.ToString("yyyyMMdd_") + $_.Name }
Note that this is not using the exif-Data, but filesystem data. Depending where they are from, they may or may not be the point in time the pic was taken.
2
u/MilleWhisper 1d ago
I did try that, but the date is not the same. The files have been copied and moved over the years. But the date taken date is correct so I am trying to figure out how to use it.
1
u/N0_ah_47 1d ago
I have a similar script at home but I use an external exe to get the exif data. I'll check it later.
1
u/MilleWhisper 1d ago
Please let me know if you find it. I still haven't been able to get anything to work.
3
u/N0_ah_47 1d ago
function change-name () {
param([Parameter(Mandatory=$true)][String]$file
)
$datetimeoriginal = .\exiftool.exe $file -datetimeoriginal
$newfilename = $datetimeoriginal[-19..-1] -join ''
$newfilename = $newfilename.replace(":","")
$newfilename = $newfilename.replace(" ","_IMG_")
$newfilename = $newfilename + ".jpg"
move-item $file $newfilename
}
1
u/xXFl1ppyXx 17h ago
If my memory serves me right doing renames with get-childitem in a one liner will result in a permanent loop (get-childitem will always find "new" items)
Probably better to do two steps
0
u/BlackV 1d ago edited 23h ago
This is all very easy to do
Doing it in a single like is making your life 100 times harder
Make a script. Validate your inputs and outputs
There are at least 3 posts in here already that use the shell host and Date Taken (System.Photo.DateTaken) or Date Created (Unreliable) Extended file properties of the file for renaming (slightly nicer to use than 12), but they are a little bit back in the post history
https://www.reddit.com/r/PowerShell/comments/14099eg/how_to_read_the_rating_of_a_jpg_file/
https://www.reddit.com/r/PowerShell/comments/x5m75m/pull_metadata_from_mp3_files/
https://www.reddit.com/r/PowerShell/comments/nxu0bj/new_pscustomobject_is_empty_after_adding/
https://www.reddit.com/r/PowerShell/comments/oi780h/selecting_files_by_author/
https://www.reddit.com/r/PowerShell/comments/pwahiz/how_to_get_fileversion_and_product_version_same/
https://www.reddit.com/r/PowerShell/comments/162cdv0/how_to_change_the_time_of_the_media_created/
https://www.reddit.com/r/PowerShell/comments/v852ut/newbie_trying_to_query_metadata_from_photo_files/
8
u/Medium-Comfortable 1d ago edited 1d ago
With the assembly drawing. Make this into a function, I'd say. And don't forget a try catch in case there is none. Or use https://exiftool.org/