Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CODE TO CHANGE ALL FILENAMES TO DATE AND TIME
#1
Hi, about two weeks ago Kevin and I worked on a code to return the date and time of all files inside a folder. That is working quite well, see prior posts.
Then I decided to split the components of date and time and to make a string used to rename all filenames to their specific date and time.
This is specially interesting, for example, if the files are images from a time lapse taken.

The script below is working well, I just wonder if there is a more elegant way to rename the time than the way I have done.
Remember, the time output is the problem hh:mmConfuseds does not work to rename files,  the ":" is making the problem.
Anyways, whoever is interested can use the script as it is, suggest an improvement to reduce the amount of lines, etc...

Thank you very much
Emerson
 
Code:
Copy      Help
str s0 s1 s2 s3 s4 s5
int i
ARRAY(str) a
GetFilesInFolder a "$Desktop$\TEST\" ""
a.sort(8)
for i 0 a.len
    Dir d.dir(a[i])
    if(d.FileSize > 0);;error handing to make sure file is not empty(if we try to access empty file, the function will error and end)
        d.FileName
        str R=CsFunc("" d.FullPath)
        if(R !="No MetaData Found")
            DateTime dt
            dt.FromStr(R)
             s0=dt.ToStr(4);; THE COMPLETE DATE and TIME (YYYY-MM-DD HH:MM:SS)
            s1=dt.ToStr(1);; THE COMPLETE DATE
            s2=dt.ToStr(6)
            s3=dt.ToStr(6)
            s4=dt.ToStr(6)
            s2.remove(2 6);; ONLY THE HOUR
            s3.remove(0 3);; HOUR AND MINUTE
            s3.remove(2 4);; ONLY THE MINUTE
            s4.remove(0 6);; ONLY THE SECOND
            s5=F"C:\Users\emers\Desktop\TEST\{s1}__{s2}-{s3}-{s4}.jpg"
            out a[i]
            out s5
             ren a[i] s5
        else
            out R
#ret
using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
public class Example
{
    private static Regex r = new Regex(":");
    //retrieves the date taken without loading the whole image
    public static string GetDateTakenFromImage(string path)
    {
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        using (Image myImage = Image.FromStream(fs, false, false))
        {
            PropertyItem propItem = null;
            try
            {
                propItem = myImage.GetPropertyItem(0x9003);
            }
            catch { }
            if (propItem != null)
            {
                string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
                return dateTaken;
            }
            else
                return "No MetaData Found";
        }
    }
}
#2
much simpler is to format the filename with timeformat
Code:
Copy      Help
int i
ARRAY(str) a
GetFilesInFolder a "$Desktop$\TEST\" ""
a.sort(8)
for i 0 a.len
,Dir d.dir(a[i])
,if(d.FileSize > 0);;error handing to make sure file is not empty(if we try to access empty file, the function will error and end)
,,;;out d.FileName
,,str R=CsFunc("" d.FullPath)
,,if(R !="No MetaData Found")
,,,DateTime dt
,,,dt.FromStr(R)
,,,_s.timeformat("C:\Users\emers\Desktop\TEST\{yyyy-MM-dd}__{HH-mm-ss}.jpg" dt)
,,,out _s
,,,ren a[i] _s
,,else
,,,out R
#ret
using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
public class Example
{
,private static Regex r = new Regex(":");
;;;;//retrieves the date taken without loading the whole image
,public static string GetDateTakenFromImage(string path)
,{
,,using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
,,using (Image myImage = Image.FromStream(fs, false, false))
,,{
,,,PropertyItem propItem = null;
,,,try
,,,{
,,,,propItem = myImage.GetPropertyItem(0x9003);
,,,}
,,,catch { }
,,,if (propItem != null)
,,,{
,,,,string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
,,,,return dateTaken;
,,,}
,,,else
,,,,return "No MetaData Found";
,,}
,}
}
#3
Hi Kevin,
yes, it is much simpler and works very well.
Thank you for the update.
Have a great night
Emerson


Forum Jump:


Users browsing this thread: 1 Guest(s)