Skip to content

Rename the files with a rule of dates.

Ali G

Asked Sep 29, 2013, 1:21 AM EDTSolved
Share
LinkedInX
I have written a Photo Renamer code which renames the photos according to their date taken. After the date I added a sequence number like 001, 002, 003,.... Finally I got the 2013.07.12.001.JPG name format. But there are photos on different dates and it is nonsense sequence number's follow the other days sequence. Every date must have its own sequence, not other day's.
You can see the problem at my photo.

How can I edit the loop part of my code in order to get such rename:

2013.07.12.001.JPG
2013.07.12.002.JPG
2013.07.12.003.JPG
2013.07.12.004.JPG
2013.07.12.005.JPG
2013.08.27.001.JPG
2013.07.11.001.JPG
2013.07.11.002.JPG
2013.07.11.003.JPG
2013.07.11.004.JPG
2013.07.11.005.JPG

My codes are here:
============================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging; //YENI
using System.IO; //YENI

namespace PhotoRenamer2
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
  DialogResult result = folderBrowserDialog1.ShowDialog();
  string pathname = @folderBrowserDialog1.SelectedPath;
  label2.Text = pathname;
  FileInfo[] files = new DirectoryInfo(pathname).GetFiles("*.JPG");
  for (int i = 0; i < files.Length; i++)
  {
  string DateAndTimePictureTaken = string.Empty;
  Image image = new Bitmap(@files[i].FullName);
  PropertyItem[] propItems = image.PropertyItems;
  foreach (PropertyItem propItem in propItems)
  {
  if (propItem.Id == 0x0132)
  {
  DateAndTimePictureTaken = (new System.Text.ASCIIEncoding()).GetString(propItem.Value);
  image.Dispose();
  string Year = DateAndTimePictureTaken.Substring(0, 4);
  string Month = DateAndTimePictureTaken.Substring(5, 2);
  string Day = DateAndTimePictureTaken.Substring(8, 2);
  string PhotoNumber = (i+1).ToString().PadLeft(3, '0');
  string newFile = String.Format("{0}.{1}.{2}.{3}.{4}", Year, Month, Day, PhotoNumber, "JPG");
  string fullNewPath = files[i].DirectoryName;
  string newFileWithPath = Path.Combine(fullNewPath, newFile);
  label2.Text = files[i].FullName;
  label3.Text = newFileWithPath;
  File.Move(files[i].FullName, newFileWithPath);
  break;
  }
  }
  }
  }

  }
}
============================================

7 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    ✓ Accepted answer0
    Hi Ali G,

    Use the code below, It's more efficient and eligant:
    ------------------------------
    C#
                Dictionary<string, DateTime> filesData = new Dictionary<string, DateTime>();
                string[] files =Directory.GetFiles(@"D:\test\");
                foreach (string file in files)
                {
                    filesData.Add(file, File.GetCreationTime(file));
                }
    
    
                DateTime dt = filesData.OrderBy(x => x.Value).FirstOrDefault().Value;
                int count = 1;
                foreach (var item in filesData.OrderBy(x => x.Value))
                {
                    string filename=string.Empty;
                    if (item.Value.Date != dt.Date)
                    {
                        count = 1;
                        dt = item.Value;
                    }
                    filename = Path.GetDirectoryName(item.Key) + "\\" + item.Value.ToString("yyyy.MM.dd.") + string.Format("{0:000}", count) + ".jpg";
    
                    File.Move(item.Key, filename);
                    count++;
                }
    ------------------------------------------

    Happy Coding :)
    0
  • 0
    Dear Sunny, These are very nice cdes. Have you written them for me or have you had them? I will change GetCreationTime(file) with ????? to get the date Taken of photo.
    0
  • Sunny  Sharma
    0
    Hi Ali G,

    I've written these codes for you itself. You don't need to change GetCreationTime(file) at all as it will get you the date photo Taken. I assume that you mean the File Creation Date by date photo Taken unless otherwise specified.


    0
  • 0
    Here is the directory in which your codes run. The first column is the date which I took the photo. The second column shows the date which I moved those photos to this directory. Your codes take reference the second column. I need the first column's date to use renaming the files. Turkish to English translation of the column names:
    Çekildigi tarih: Date taken
    Olusturma tarihi: Creation date
    0
  • 0
    I have changed this line:

    filesData.Add(file, File.GetCreationTime(file));


    to this line:

    DateTime d=File.GetLastWriteTime(filepath);



    Result is OK but i couldn't understand I hope it runs for every file... Thank you Sunny...
    0
  • Sunny  Sharma
    0
    Actually, GetCreationTime(file) gives you the Date when the file is created inside a particular folder. You'll see that the Date Created property changes for new copy when you Copy any file to a new location, but it doesn't when Move, because you're not creating a new file, instead you're jut changing the path.

    GetLastWriteTime(file) gives you the Date when the file was last modified. It would be same as Date Created if it's not modified after it's created.

    So, in your case, GetLastWriteTime(file) will work fine assuming that photos not modified after it's created.

    Hope you got the point :)
    0
  • 0
    Exactly it is true what you told me in last post. Some of my photos changed and I couldn't rename them correctly. I like your codes but I need to use Date taken which I use in my first post. But integration of your code and my DateAndTimePictureTaken = (new System.Text.ASCIIEncoding()).GetString(propItem.Value); code line will be very hard.
    0