Ali G

Ali G

  • NA
  • 51
  • 38.7k

Renaming As YYYY.MM.DD.001.JPG and Moving to YYYY.MM.DD

Oct 1 2013 10:12 AM
 
I asked similar before but still I don't have a usable answer. My codes read JPG file's date taken data and rename the files like YYYY.MM.DD.001.JPG . But every date must have its own sequence numbers.

And I need to move every file to a directory which has same name with the filename's date part (YYYY.MM.DD).

WOULD YOU CORRECT THESE CODES:

======================================
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.IO;
using System.Drawing.Imaging;

namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
string pathname;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

DialogResult result = folderBrowserDialog1.ShowDialog();
pathname = @folderBrowserDialog1.SelectedPath;
int JPGcount = Directory.GetFiles(@pathname, "*.JPG", SearchOption.AllDirectories).Length;
}

private void button2_Click(object sender, EventArgs e)
{
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"); // PhotoNumber MUST START FOR EVERY DATE.
string fullNewPath = files[i].DirectoryName;
string newFileWithPath = Path.Combine(fullNewPath, newFile);
File.Move(files[i].FullName, newFileWithPath); //NEEDED TO MOVE TO A FOLDER NAMED YYYY.MM.DD
break;
}
}
}
}
}
}
======================================

Answers (3)