How to rename photos to the date taken
ฝัง
- เผยแพร่เมื่อ 20 ส.ค. 2024
- $shell = New-Object -ComObject shell.application
$ukCulture = [Globalization.CultureInfo]'en-GB'
Get-ChildItem | ForEach{
$folder = $shell.NameSpace($_.DirectoryName)
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /:]")
$datetime = [DateTime]::Parse($RawDate,$ukCulture)
$DateTaken = $datetime.ToString("yyyy-MM-dd HH.mm")
Rename-Item $_.FullName ($DateTaken + $_.Extension)}
Find out what the code is for your country: docs.microsoft...
Code for if you have multiple files with the same date and time
$shell = New-Object -ComObject shell.application
$ukCulture = [Globalization.CultureInfo]'en-GB'
Get-ChildItem | ForEach{
$folderpath = $_.DirectoryName
$folder = $shell.NameSpace($_.DirectoryName)
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /:]")
$datetime = [DateTime]::Parse($RawDate,$ukCulture)
$DateTaken = $datetime.ToString("yyyy-MM-dd HH.mm")
$number = 1
$newpath = $folderpath + "\" + $DateTaken + "_" + $number + ".jpg"
If(Test-Path -Path $newpath){
while ((Test-Path -Path $newpath) -eq $true)
{$newpath = Join-Path $folderpath ($DateTaken + "_" + $number + $_.Extension)
$number+=1}
Rename-Item $_.pspath -NewName $newpath}
else {Rename-Item $_.pspath -NewName $newpath} }
Thank you Karina, you saved so much of my time!
This is great!
Hi Karina, thank you so much for this great tutorial! I am using the code provided in the pinned comment for the multiple files with the same date and time. It works well overall except that for most, if not all, of the files that get renamed, it skips a number at the end. For example, if I have 12 files with the same date and time, it gives the correct suffix for the first 11 (e.g. "_1", "_2"..."_10" and "_11") and then assigns "_13" to the final file even though it should be "_12". Any idea as to why this is happening? Thank you for any additional assistance! - Maegan
Thank you so much for this briliant solution and excellent video. Can we extend the date to include seconds i.e. HH.mm.ss?
Hi I want to do this formula but for Date Modified as per the other YT video. But the other video just links to this videa when there are multiple files with the same date and time for Date Modified. (I can't do Date Taken as it's mostly missing/incorrect.)
Gives answer in first 30 seconds, offers the rest of the video for those interested in knowing what it means to learn. THIS is how ALL videos should be structured. Thank you. :)
literally. this is unironically one of the best instructional videos on the internet. and I've watched 1000s!!!
I really appreciate your video. This is a solution to a problem I've been wanting to solve -- renaming my camera photos to include the date taken. There are shareware apps to do this but they only do my jpg files, not the associated raw files. Your approach works great. Doing it via PowerShell is very cool. And the way you provide a detailed bottoms-up explanation is absolutely fantastic. You explain this so well. THANK YOU.
Exception calling "Parse" with "2" argument(s): "String was not recognized as a valid DateTime."
Doesn't work
Same: Exception calling "Parse" with "2" argument(s): "String was not recognized as a valid DateTime."
Love it so much with answer at the very beginning followed by detailed explanation. Then user could adjust according to their own situation!
Brilliant piece of work. Love your clarity of thinking and presentation
I actually love you so much for the super fast, straight to the point explanation!
Great video! Thank you for sharing it. If there are photos taken at the exact same date, hour and minute, the code will produce an error. To avoid this issue, you can check if the new file name already exists and add a number after the file name. One way to modify the code to achieve this would be:
$shell = New-Object -ComObject shell.application
$ukCulture = [Globalization.CultureInfo]'en-GB'
Get-ChildItem | ForEach-Object {
$folder = $shell.NameSpace($_.DirectoryName)
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /:]")
$datetime = [DateTime]::Parse($RawDate,$ukCulture)
$DateTaken = $datetime.ToString("yyyy-MM-dd HH.mm")
$NewName = $DateTaken + $_.Extension
# Check if the file name already exists
if (Test-Path ($_.DirectoryName + '\' + $NewName)) {
$counter = 1
$NewNameBase = $DateTaken + "_$counter" + $_.Extension
while (Test-Path ($_.DirectoryName + '\' + $NewNameBase)) {
$counter++
$NewNameBase = $DateTaken + "_$counter" + $_.Extension
}
$NewName = $NewNameBase
}
Rename-Item $_.FullName $NewName
}
JL, I had an issue with Karina's coding as I have many photos taken at the same date and time. I tried your code and it did the same thing Karina's did. Changes them all to the same date even though some were taken on other dates. Any ideas ? Thanks!
Thank you for making my family photo project easier. I have about 2 thousand photos that I want to put on a time line. :-)
(This is a wonderfully explained tutorial; thank you)
Why do all date-taken strings end in :00 (seconds)?
Nice script.
For the U.S, change the second line to be en-US instead of en-GB
The problem with this script occurs when you have several photos that were taken within the same minute.
The script will change the first one it encounters and leaves the other ones alone.
Hi, yes that's exactly my issue as I mentioned above.
This will add a number on the end of the file names which will count up by one each time. The number won't restart with each date, it will just keep counting up for every file in the folder.
$shell = New-Object -ComObject shell.application
$ukCulture = [Globalization.CultureInfo]'en-GB'
$number = 1
Get-ChildItem | ForEach{
$folder = $shell.NameSpace($_.DirectoryName)
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /:]")
$datetime = [DateTime]::Parse($RawDate,$ukCulture)
$DateTaken = $datetime.ToString("yyyy-MM-dd HH.mm")
Rename-Item $_.FullName ($DateTaken + "_{0:0000}$($_.Extension)" -f $number++)}
@@karinaadcock : I just tried it. WORKS GREAT! Thanks.
Thank you this is so helpful! However, in my case, some of the photos do not have the parameter of date taken, and an error occurs. How can I fix this? I prefer not renaming these photos that do not have date taken information.🤔
Karina, Thank you for these videos. I have used the "LastWriteTime" code and it worked perfectly. I have some files that I need to use the Date Taken as you explain on this video.
I get and error message for some of the files that have the same TIME. I believe it's because it's not considering the seconds (although the seconds are diferent and showing on windows)
Do you know why it's not renaming the file using the seconds, but only HH and mm?
Thank so much for the clear instructions and script. It worked well for me. I found your video after Magic File Renamer was not properly working for all the files I had in a folder.
Unfortunately downloading from Google Photos or Apple iCloud does not keep the file date as date taken.
Thank you so much!!! And thank you for providing the solution in the very beginning of the video!
Thank you so much for this.
it solved so many problems for me
When I look at my folders in file explorer there is a column named "Date", which is different then any of the other dates found in the details, that's not the time & date listed from DIR command (same "LastWriteTime" from "get-childitem"). The one in file explorer could be the date created on the system and stored at low level in directory entry, that's the one useful for me ! I can't find a way to get to it though ? Yes, i checked all the individual labeled as some date from the list generated by "0..287|Foreach-Object {'{0} = {1}' -f $_, $shellfolder.GetDetailsOf($null,$_) }"
And many thanks, solid and instructive piece of work !
Hello Pierre, did you find a solution for this issue? I'm having the same problem when the "date taken" is not accurate on windows. Otherwise, I use the other code given by Karina, when "LastWriteTime" is good. If you can share the solution, it'd be greatly appreciated
Question - Is it possible to retain the original file name and append the date taken onto it?
Example: convert 'IMG_3650.jpg' to 'IMG_3650 2023-01-02 17.52.jpg'
Or, better yet, convert to: '2023-01-02 17.52 IMG_3650.jpg'
One final question - is it possible to have the time show with colon - '17:52' instead of '17.52'?
Excellent video - thank you!
Figured out how to keep original name:
Rename-Item $_.FullName ($DateTaken + " " + $_.BaseName + $_.Extension)}
Can't figure out how to keep the colon in the time format HH:mm...
Excellent explanation. Thank you.
Thank you so much for this. I have lots of pictures that are missing a Date Taken attribute. Is there a way to add an IF statement so that if the Date Taken attribute is missing it'll use the file date?
well done, and thank you!
Thanks, this helped me a lot :)
This is amazing. Thank you so much!
Hi Karina, thanks for your video and worked like a treat. i have a mix of Videos and Pictures in my folder. is there a way of adding the File type into the name as well so when i look at my images in Large preview mode i will know if its a video or a pic?
Thank you so much for such helpful video! I have one question:
I don't use Georgian calendar, but after "Get-ChildItem" command in PowerShell the "LastWriteTime" is in Georgian calendar.😓 Is it possible to change photos name in any other Windows supported calendars using this method? It'll be a great help🥺
Thank you, Thank You and Thank you!
Thank you lots for this!
Hi Karina, thanks for the very informative video. I am a non-IT guy and have no knowledge nor skills in script writing. I follow every step but my computer showed errors right from the start. Is there a limit to the version of windows I am operating in? For your advice, please.
Can you please tell the code to add multiple files with the same date and time 'modified date' as a file name instead of the 'created date'
great video, thanks
Hi Karina! I am glad I found this tutorial. I am not an expertt at all with PowerShell, but wanted to give it a try. Hope you can help me tyring to figure out an error I am getting with the Parse method as it is not working due to an error in the date being passed (RawDate is not recognized). I am wokring with en-CA and my dates are YYYY-MM-dd nut when getting the date it removes the dashes (-) and then converting to $datetime does not work.
This is what I am getting back in the $RawDatre variable: 20041215 12:16 PM
Error is: Exception calling "Parse" with "2" argument(s): "String '20041215 12:16 PM' was not recognized as a valid DateTime."
InvalidOperation:
Try adding dashes to the things you don't want to replace -Replace "[^\w /:-]"
Karina, i've found this extremely helpful so thanks very much.
The only lasting issue i have is that when i run the script some files return an error as they are part of a 'burst' of pictures taken. I tried to add seconds to the time which i hoped would solve the problem but it just returns zero's. Is there a quick add-on to the above that might an extra +1, +2 or +3 etc to differentiate the files perhaps? I am using your script and doing this manually at the moment but would love to be fully automated.
This will add a number on the end of the file names which will count up by one each time. The number won't restart with each date, it will just keep counting up for every file in the folder.
$shell = New-Object -ComObject shell.application
$ukCulture = [Globalization.CultureInfo]'en-GB'
$number = 1
Get-ChildItem | ForEach{
$folder = $shell.NameSpace($_.DirectoryName)
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /:]")
$datetime = [DateTime]::Parse($RawDate,$ukCulture)
$DateTaken = $datetime.ToString("yyyy-MM-dd HH.mm")
Rename-Item $_.FullName ($DateTaken + "_{0:0000}$($_.Extension)" -f $number++)}
@@karinaadcock Thanks Karina.
I live in Canada and when I paste the script it says "The string is missing the terminator: '."
Awesome help!
How would be the code for renaming videos to the media created?
i believe its just changing the line $RawDate line to the following.
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),4) -Replace "[^\w /:]")
Hi there - thanks for this but I really needed this code for audio files, and it doesn't appear to work. Does it only work on photos?
Might there be a code to change all 'date created' to 'date taken' on multiple files with all different dates?
not working in may 2024
Doesn't work for me. It renames the first file to just ".jpg", then call an error on the rest, as the name is already in use.
There's an issue with your code. I ran some of the code, that you created during the video.
Already at..
$folder = (Get-ChildItem | Select-Object -Index 0).DirectoryName
$shell = New-Object -ComObject shell.application
$shellfolder = $shell.NameSpace($folder)
$shellfolder.GetType()
.. I got an error. After fiddling a bit around, it turns out, your code example doesn't like, that there's a sub-folder in the folder with the images.
i get this error for all files:
Exception calling "Parse" with "2" argument(s): "Strengen blev ikke genkendt som en gyldig DateTime."
At line:4 char:1
+ $datetime = [DateTime]::Parse($RawDate,$ukCulture)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
You cannot call a method on a null-valued expression.
At line:5 char:1
+ $DateTaken = $datetime.ToString("yyyy-MM-dd HH.mm")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The "Strengen blev ikke genkendt som en gyldig DateTime" part, translates to "The string was not recognized, as a valid DateTime". I'm not sure, if it's because my PC runs Date and time, as dd-mm-yyyy (da-DK), and not the "English/American" version, with mm-dd-yyyy.
So my problem is here:
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /:]")
Your output of $RawDate is in this format: "11/04/2018 19:46".
Mine is like this: "11042018 19:46" (no slashes).
That's why your code doesn't work for me.
Further development.
I found out, that I could do...
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),12) -Replace "[^\w /: /-]")
$datetime = [DateTime]($RawDate)
$datetime = [DateTime]::Parse($RawDate,$ukculture)
$datetime}
... and that gives me output like "21. februar 2018 15:16:00" for $datetime.
Problem is, I still get an error "Cannot convert value "21-02-2018 15:16" to type "System.DateTime".
Is possible to make code that rename into date taken but with seconds? It would be very helpful. Thank you.
why Video file change to JPG? without media created date
how could I add an other line of code, like after this script has renamed the file, is should for backup reasons copy the newnamed files into a new folder. Does anybody know? Its an amazing Video/Explanation Karina!!! Thanks a million!!
xcopy "source folder" "destination folder" will copy all files from one folder to another, for example,
xcopy "C:\Users\Karina Adcock\Desktop\photos" "C:\Users\Karina Adcock\Desktop\backup"
If the backup folder hasn't been created yet it will ask you if its a file or directory. Then you put D for directory and it will make the folder and copy the files to it.
Any way to do this with a batch file?
Do you by any chance know how to do this for a mac?
Hello! this been very useful, I used for a lot even well explained for a complete unkown of powershell with zero experience coding I understood a lot. I have a question for anybody can help with an extension.
I have many photos without date of capture. I would like then when there is no info for capture date, take the minimum date between Date, Creation Date and Modify Date.
By trying different numbers in GetDetailsOf () I found creation date is "4", modifyDate is "3" and I am stil missing regular Date access and the skills to evaluate the minimum of them in the lack of Capture Date "12" and rewrite the original names with the resulting ones.
Thanks for reading so far and I appreciate this video and anybody giving a help with this.
Atm I manage to improve selecting only desire format files with Where:
Get-ChildItem | Where{$_.Extension -like ".jpg" -or $_.Extension -like ".jpeg" -or $_.Extension -like ".gif" -or $_.Extension -like ".png" -or $_.Extension -like ".mp4" -or $_.Extension -like ".mov"} | ForEach{
But still there is a problem with files with no capture datetime information, they get name replaced with datetimes of other files (which are nonrelated and wrong)
How do you do the same with Videos if I want to use "Media Created" date? I played around with the RawDate and wasn't able to make it work.
i believe its just changing the line $RawDate line to the following.
$RawDate = ($folder.GetDetailsOf($folder.ParseName($_.Name),4) -Replace "[^\w /:]")
@@scottdriiver it is not working for video, how i change to media created?
@@doretsion3593 you need to change part of the script to what I mentioned above. (Testing my brain as it was 7 months ago since I posted this comment 😂)
@@scottdriiver i tried it with limited success, some file success and some not, also I have 1 folder with pictures and movies - how can i do it for both at 1 time?
Hello there, I've tried editing the code to be used in Canada, but I keep getting errors. What would I have to change to have it work here? Thank you!
Same here! Can't get it to work. $datetime throws an error
I'm in Canada as well and was also getting errors. I had to follow the tutorial in full to find out, but it seems it was because the date in my Windows is separated with - instead of / like Karina shows in her video.
This means I had to write "[^\w -:]" instead of "[^\w /:]" since my dates didn't have any slashes.
I know it's been months since your question but hope that helps anyone popping by!
@@tapeton Thanks a lot saved me so much time
@@tapeton Thanks a lot!!!
Hi Katrina, thank you for posting this! I have tried to alter the code for my situation but it comes up with errors. Can you please help? I changed UK to AU throughout; and wanted to change from "Date Taken" (I have no data for these files) to "Date Modified" so I updated the text and changed the attribute from 12 to 3. It shows lots of errors though :D
Have you tried using this method th-cam.com/video/v82gkPYZa7Y/w-d-xo.html
@@karinaadcock thank you for your response, I have been able to resolve for my situation. Thank you again for your clear and informative style! Simply wonderful. I am most grateful and will pass your videos along to others.