How can I change the UTC time to my time zone?

Advanced Renamer forum
#1 : 14/12-22 07:56
hardy
hardy
Posts: 3
Hi,

I need to add the video creation time to the file name, but the time obtained through mediainfo is 8 hours different from my time zone.

file name: VID_20210130_112311.mp4
media creat time: 2021_01_30 03_23_29

I want to convert the time to this format: 2021-1-30&11-23-29

I found a script here, but there is an error on line 4.
https://www.advancedrenamer.com/forum_thread?for um_id=11037
~ Invalid input pre script: uncaught: 'invalid return(line 4)' ~

I modified the script appropriately to match the format of mediaiinfo.

//
match = item.name.match(/^(.*)(\d{4}_\d{2}_\d{2}) (\d{2})_(\d{2})_(\d{2})$/);
epoch = Date.parse(match[2] + 'T' + match[3] + ':' + match[4] + ':' + match[5]);
date = new Date(epoch + 8 * 3600000);
return match[1]
+ date.getFullYear()
+ "-" + ("0" + (date.getMonth() + 1)).slice(-2)
+ "-" + ("0" + date.getDate()).slice(-2)
+ "&" + ("0" + date.getHours()).slice(-2)
+ "-" + ("0" + date.getMinutes()).slice(-2)
+ "-" + ("0" + date.getSeconds()).slice(-2);
//

Thanks.

I uploaded the script screenshot:
https://i.ibb.co/FDVj1T1/20221214193412.jpg


14/12-22 07:56 - edited 14/12-22 12:53
#2 : 21/12-22 10:07
David Lee
David Lee
Posts: 1071
You have several problems here.

1) You have placed your code in the pre-batch script, which is executed only once before cycling through the files in the List. The code should be entered in the main script window so that it will operate on each filename. Unlike the main script, the pre-batch script is not a function so does not require a return statement - this is the cause of the "invalid return" error.

2) item.name returns the original filename - you need to use item.newName here in order to recover the name returned by the previous method. However item.newName returns the filename INCLUDING the extension so you will need to replace "$" (end of string) in the match statement with "\." (extension separator).

3) Date.parse() is expecting a string in the format "2021-01-30T03:23:29", so you need to match Year, Month & Day separately.

4) Finally "|" is an illegal character in a Windows filename so even if your script is correct and Advanced Renamer is showing the correct filenames it will display an error message and affected files will be skipped when you run the batch.

match = item.newName.match(/^(.*)(\d{4})_(\d{2})_(\d{2}) (\d{2})_(\d{2})_(\d{2})\./);
if (match[0]) {
epoch = Date.parse(match[2] + '-' + match[3] + '-' + match[4] + 'T' + match[5] + ':' + match[6] + ':' + match[7]);
date = new Date(epoch + 8 * 3600000);
return match[1]
+ date.getFullYear()
+ "-" + ("0" + (date.getMonth() + 1)).slice(-2)
+ "-" + ("0" + date.getDate()).slice(-2)
+ "&" + ("0" + date.getHours()).slice(-2)
+ "-" + ("0" + date.getMinutes()).slice(-2)
+ "-" + ("0" + date.getSeconds()).slice(-2);
}

See also my reply to the recent thread: https://www.advancedrenamer.com/forum_thread?for um_id=13251







21/12-22 10:07 - edited 21/12-22 10:14
#3 : 22/12-22 04:14
hardy
hardy
Posts: 3
Reply to #2:

Thank you very much, I have tested the three scripts you wrote, and there will be invalid return errors.

I found that all errors occur in the return line of code.

Screenshots of these errors report.

https://i.ibb.co/RD6P7NJ/Change-hour-format-from -UTC-to-GMT-7.jpg
https://i.ibb.co/DtJQwPX/help-about-changing-day -and-hour-of-thousand-of-files.jpg
https://i.ibb.co/ccQzWJW/new-name.jpg
https://i.ibb.co/KLFw1R1/invalid-return.jpg


22/12-22 04:14
#4 : 22/12-22 11:37
David Lee
David Lee
Posts: 1071
Reply to #3:

Read the error messages:

"Invalid input pre script:" indicates an error in the Pre-batch script.

The image linked in your original post shows that you had entered the same code in both the main and pre-batch scripts so I assume that you have repeated the mistake in your latest attempts.

As I pointed out in my previous reply, the pre-batch script is NOT a function so a return statement will throw an error. The main code should be entered ONLY in the main script window. The purpose of the pre-batch script is to set up variables and carry out operations that need to be executed a single time before the main script function is iterated though all the filenames in the list.

Open the pre-batch script in each case and delete all the code and try again.

Note that if you open a new script method the last-used code will be displayed - and this includes the pre-batch script, so you should always check that you haven't left any unwanted code in there.

See the User Guide:
https://www.advancedrenamer.com/user_guide/metho d_script
https://www.advancedrenamer.com/user_guide/examp le_scripting


22/12-22 11:37 - edited 22/12-22 11:38
#5 : 22/12-22 16:19
hardy
hardy
Posts: 3
Reply to #4:
Thanks again, I finally got it and the script executed successfully!


22/12-22 16:19