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;
}
}
}
}
}
}
======================================
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.ShowDialo
pathname = @folderBrowserDialog1.Selected
int JPGcount = Directory.GetFiles(@pathname, "*.JPG", SearchOption.AllDirectories).L
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo[] files = new DirectoryInfo(pathname).GetFil
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()).G
image.Dispose();
string Year = DateAndTimePictureTaken.Substr
string Month = DateAndTimePictureTaken.Substr
string Day = DateAndTimePictureTaken.Substr
string PhotoNumber = (i + 1).ToString().PadLeft(3, '0');
string newFile = String.Format("{0}.{1}.{2}.{3}
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;
}
}
}
}
}
}
==============================
Sunny SharmaPosted Oct 1, 2013, 1:23 PM
Here is the code you want:
-----------------------------------------------------------------
string[] files =Directory.GetFiles(@"D:\test\","*.jpg");
foreach (string file in files)
{
string DateAndTimePictureTaken = string.Empty;
Image image = new Bitmap(file);
PropertyItem[] propItems = image.PropertyItems;
foreach (PropertyItem propItem in propItems)
{
if (propItem.Id == 0x0132)
{
DateAndTimePictureTaken = (new System.Text.ASCIIEncoding()).GetString(propItem.Value);
filesData.Add(file, DateTime.ParseExact(DateAndTimePictureTaken.Substring(0, 10), "yyyy:MM:dd", CultureInfo.InvariantCulture));
image.Dispose();
break;
}
}
}
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++;
}
--------------------------------------------------------------
implement this code on button_click event.
HTH :)
Happy Coding.
Sunny SharmaPosted Oct 3, 2013, 7:21 AM
Ali GPosted Oct 3, 2013, 7:11 AM