From d2afb454ce7df398b7e63b1056ab3043b2acaa0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Wed, 21 Apr 2021 23:23:49 +0200 Subject: [PATCH] Fixat numrering i sllaskkatalog --- PictureReposit/Form1.cs | 384 ++++++++++++++++++++++------------------ 1 file changed, 215 insertions(+), 169 deletions(-) diff --git a/PictureReposit/Form1.cs b/PictureReposit/Form1.cs index e7c6e50..705ccd1 100644 --- a/PictureReposit/Form1.cs +++ b/PictureReposit/Form1.cs @@ -15,195 +15,241 @@ namespace PictureReposit public partial class Form1 : Form { - private bool Deleting = false; - public Form1() + private bool Deleting = false; + public Form1() { InitializeComponent(); - RegistryKey PictureHandling = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Tfoman\PictureReposit"); - if (PictureHandling == null) - { - txtRootDir.Text = @"D:\Bildlabb\LabRotCat"; - } - else - { - txtRootDir.Text = (string)PictureHandling.GetValue(@"RootMap"); - } + RegistryKey PictureHandling = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Tfoman\PictureReposit"); + if (PictureHandling == null) + { + txtRootDir.Text = @"D:\Bildlabb\LabRotCat"; + } + else + { + txtRootDir.Text = (string)PictureHandling.GetValue(@"RootMap"); + } - } + } private void btnFileDialog_Click(object sender, EventArgs e) { - openFileDialog1.Title = "Välj bild-fil!"; - openFileDialog1.InitialDirectory = "C:\\"; - openFileDialog1.Filter = "Picture files (*.jpg)|*.jpg|All files (*.*)|*.*"; - openFileDialog1.FilterIndex = 1; - openFileDialog1.RestoreDirectory = true; - openFileDialog1.ShowDialog(); - openFileDialog1.Multiselect = false; - string fName = openFileDialog1.FileName; - if (fName.Trim() != "") - { - int fNamePos = fName.LastIndexOf(@"\") + 1; - lblFileName.Text = fName.Substring(fNamePos); - lblPath.Text = fName.Substring(0, fNamePos); - listLocalFiles(); + openFileDialog1.Title = "Välj bild-fil!"; + openFileDialog1.InitialDirectory = "C:\\"; + openFileDialog1.Filter = "Picture files (*.jpg)|*.jpg|All files (*.*)|*.*"; + openFileDialog1.FilterIndex = 1; + openFileDialog1.RestoreDirectory = true; + openFileDialog1.ShowDialog(); + openFileDialog1.Multiselect = false; + string fName = openFileDialog1.FileName; + if (fName.Trim() != "") + { + int fNamePos = fName.LastIndexOf(@"\") + 1; + lblFileName.Text = fName.Substring(fNamePos); + lblPath.Text = fName.Substring(0, fNamePos); + listLocalFiles(); - for (int i = 0; i < lstPicFiles.Items.Count; i++) - { - if (lstPicFiles.Items[i].ToString() == lblFileName.Text) - { - lstPicFiles.SelectedIndex = i; - break; - } - } + for (int i = 0; i < lstPicFiles.Items.Count; i++) + { + if (lstPicFiles.Items[i].ToString() == lblFileName.Text) + { + lstPicFiles.SelectedIndex = i; + break; + } + } - } - } + } + } - private void listLocalFiles() - { - DirectoryInfo di = new DirectoryInfo(lblPath.Text); - FileInfo[] files = di.GetFiles("*.jpg"); - lstPicFiles.Items.Clear(); - for (int i = 0; i < files.Length; i++) - { - lstPicFiles.Items.Add(files[i].Name); - } - files = di.GetFiles("*.jpeg"); - for (int i = 0; i < files.Length; i++) - { - lstPicFiles.Items.Add(files[i].Name); - } - btnReOrganize.Enabled = true; - } + private void listLocalFiles() + { + DirectoryInfo di = new DirectoryInfo(lblPath.Text); + FileInfo[] files = di.GetFiles("*.jpg"); + lstPicFiles.Items.Clear(); + for (int i = 0; i < files.Length; i++) + { + lstPicFiles.Items.Add(files[i].Name); + } + files = di.GetFiles("*.jpeg"); + for (int i = 0; i < files.Length; i++) + { + lstPicFiles.Items.Add(files[i].Name); + } + btnReOrganize.Enabled = true; + } private void lstPicFiles_SelectedIndexChanged(object sender, EventArgs e) { - if (!Deleting) - { - string fName = ((ListBox)sender).SelectedItem.ToString(); - lblFileName.Text = fName; - Image actPic = Image.FromFile(lblPath.Text + fName, true); - lblCreated.Text = "Skapad: " + TakenDate(actPic); - int w = actPic.Width; - int h = actPic.Height; - int pw = pictureBox1.Width; - int ph = pictureBox1.Height; - pictureBox1.SizeMode = PictureBoxSizeMode.Normal; - Bitmap to_bm = new Bitmap((Image)actPic.Clone(), w * ph / h, ph); - pictureBox1.Image = to_bm; - actPic = null; + if (!Deleting) + { + string fName = ((ListBox)sender).SelectedItem.ToString(); + lblFileName.Text = fName; + Image actPic = Image.FromFile(lblPath.Text + fName, true); + lblCreated.Text = "Skapad: " + TakenDate(actPic); + int w = actPic.Width; + int h = actPic.Height; + int pw = pictureBox1.Width; + int ph = pictureBox1.Height; + pictureBox1.SizeMode = PictureBoxSizeMode.Normal; + Bitmap to_bm = new Bitmap((Image)actPic.Clone(), w * ph / h, ph); + pictureBox1.Image = to_bm; + actPic = null; - } - } + } + } - private string TakenDate(Image actPic) - { - string sPropDatTaken = ""; - for (int i = 0; i < actPic.PropertyIdList.Length; i++) - { - if (actPic.PropertyIdList[i] == 306) - { - for (int j = 0; j < actPic.PropertyItems[i].Len; j++) - { - sPropDatTaken += Convert.ToChar(actPic.PropertyItems[i].Value[j]); - } - } - } - //actPic.Dispose(); - return sPropDatTaken; - } + private string TakenDate(Image actPic) + { + string sPropDatTaken = ""; + for (int i = 0; i < actPic.PropertyIdList.Length; i++) + { + if (actPic.PropertyIdList[i] == 306) + { + for (int j = 0; j < actPic.PropertyItems[i].Len; j++) + { + sPropDatTaken += Convert.ToChar(actPic.PropertyItems[i].Value[j]); + } + } + } + //actPic.Dispose(); + return sPropDatTaken; + } private void btnReOrganize_Click(object sender, EventArgs e) { - bool error_upd = false; - if (txtRootDir.Text.Trim().Substring(txtRootDir.Text.Trim().Length - 1) != @"\") - txtRootDir.Text = txtRootDir.Text.Trim() + @"\"; - DirectoryInfo di = new DirectoryInfo(txtRootDir.Text); - if (!di.Exists) di.Create(); - Deleting = true; - pictureBox1.Image = null; - pictureBox1.Refresh(); - for (int i = lstPicFiles.Items.Count - 1; i > -1; i--) - { - string lFileName = lstPicFiles.Items[i].ToString().Trim(); - string fName = lblPath.Text.Trim() + lFileName; - lblFileName.Text = fName; - Image actIm = Image.FromFile(fName, true); - int w = actIm.Width; - int h = actIm.Height; - int pw = pictureBox1.Width; - int ph = pictureBox1.Height; - pictureBox1.SizeMode = PictureBoxSizeMode.Normal; - Bitmap to_bm = new Bitmap((Image)actIm.Clone(), w * ph / h, ph); - pictureBox1.Image = to_bm; - string td = TakenDate(actIm); + bool error_upd = false; + bool slaskDir = false; + int slaskFilNr = 0; + if (txtRootDir.Text.Trim().Substring(txtRootDir.Text.Trim().Length - 1) != @"\") + txtRootDir.Text = txtRootDir.Text.Trim() + @"\"; + DirectoryInfo di = new DirectoryInfo(txtRootDir.Text); + if (!di.Exists) di.Create(); + Deleting = true; + pictureBox1.Image = null; + pictureBox1.Refresh(); + for (int i = lstPicFiles.Items.Count - 1; i > -1; i--) + { + string lFileName = lstPicFiles.Items[i].ToString().Trim(); + string fName = lblPath.Text.Trim() + lFileName; + lblFileName.Text = fName; + Image actIm = Image.FromFile(fName, true); + int w = actIm.Width; + int h = actIm.Height; + int pw = pictureBox1.Width; + int ph = pictureBox1.Height; + pictureBox1.SizeMode = PictureBoxSizeMode.Normal; + Bitmap to_bm = new Bitmap((Image)actIm.Clone(), w * ph / h, ph); + pictureBox1.Image = to_bm; + string td = TakenDate(actIm); - actIm.Dispose(); - if (td.Trim() != "") - { - lblCreated.Text = "Skapad: " + td; - td = td.Substring(0, 10); - DateTime dttd = DateTime.Parse(td.Replace(":", "-")); - string subDir1 = dttd.Year.ToString("0000") + "-" + dttd.Month.ToString("00") + @"\"; - string subDir2 = dttd.Year.ToString("0000") + "_" + dttd.Month.ToString("00") + "_" + dttd.Day.ToString("00") + @"\"; - DirectoryInfo dis = new DirectoryInfo(di.FullName + subDir1); - if (!dis.Exists) dis.Create(); - DirectoryInfo diss = new DirectoryInfo(dis.FullName + subDir2); - if (!diss.Exists) diss.Create(); + actIm.Dispose(); + if (td.Trim() != "") + { + lblCreated.Text = "Skapad: " + td; + td = td.Substring(0, 10); + DateTime dttd = DateTime.Parse(td.Replace(":", "-")); + string subDir1 = dttd.Year.ToString("0000") + "-" + dttd.Month.ToString("00") + @"\"; + string subDir2 = dttd.Year.ToString("0000") + "_" + dttd.Month.ToString("00") + "_" + dttd.Day.ToString("00") + @"\"; + DirectoryInfo dis = new DirectoryInfo(di.FullName + subDir1); + if (!dis.Exists) dis.Create(); + DirectoryInfo diss = new DirectoryInfo(dis.FullName + subDir2); + if (!diss.Exists) diss.Create(); - FileInfo fi = new FileInfo(fName); - //Tommy - fi.CopyTo(diss.FullName + lFileName); - error_upd = false; - do - { - error_upd = false; - try - { - lstPicFiles.BeginUpdate(); - } - catch - { - error_upd = true; - } - } - while (error_upd); - lstPicFiles.Items.RemoveAt(i); - lstPicFiles.EndUpdate(); - lstPicFiles.Refresh(); - lblCreated.Refresh(); - lblFileName.Refresh(); - pictureBox1.Refresh(); - this.Refresh(); - } - } - Deleting = false; - lblCreated.Text = ""; - lblFileName.Text = ""; - lblPath.Text = ""; - btnReOrganize.Enabled = false; - } + FileInfo fi = new FileInfo(fName); + //Tommy + fi.CopyTo(diss.FullName + lFileName); + error_upd = false; + do + { + error_upd = false; + try + { + lstPicFiles.BeginUpdate(); + } + catch + { + error_upd = true; + } + } + while (error_upd); + lstPicFiles.Items.RemoveAt(i); + lstPicFiles.EndUpdate(); + lstPicFiles.Refresh(); + lblCreated.Refresh(); + lblFileName.Refresh(); + pictureBox1.Refresh(); + this.Refresh(); + } + else + { + DirectoryInfo dis = new DirectoryInfo(di.FullName + "NoDateInExif\\"); + if (!slaskDir) + { + if (!dis.Exists) + { + dis.Create(); + set_Last_number(dis.FullName, 0); + } + else + { + slaskFilNr = get_Last_number(dis.FullName); + } + slaskDir = true; + } - private void txtRootDir_TextChanged(object sender, EventArgs e) - { - //Software\Tfoman\PictureReposit - RegistryKey Tfoman = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Tfoman", true); - RegistryKey PictureHandling = Tfoman.OpenSubKey(@"PictureReposit", true); - if (PictureHandling == null) - { - if (Tfoman == null) - { - Tfoman = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Tfoman"); - } - else - PictureHandling = Tfoman.CreateSubKey(@"PictureReposit"); - } - else - { - PictureHandling.SetValue(@"RootMap", txtRootDir.Text); - } - } + FileInfo fi = new FileInfo(fName); + //Tommy + slaskFilNr++; + fi.CopyTo(dis.FullName + slaskFilNr.ToString() + "_" + lFileName); + } + } + Deleting = false; + lblCreated.Text = ""; + lblFileName.Text = ""; + lblPath.Text = ""; + btnReOrganize.Enabled = false; + if (slaskFilNr > 0) + { + set_Last_number(di.FullName + "NoDateInExif\\", slaskFilNr); + } + } + + private void txtRootDir_TextChanged(object sender, EventArgs e) + { + //Software\Tfoman\PictureReposit + RegistryKey Tfoman = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Tfoman", true); + RegistryKey PictureHandling = Tfoman.OpenSubKey(@"PictureReposit", true); + if (PictureHandling == null) + { + if (Tfoman == null) + { + Tfoman = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Tfoman"); + } + else + PictureHandling = Tfoman.CreateSubKey(@"PictureReposit"); + } + else + { + PictureHandling.SetValue(@"RootMap", txtRootDir.Text); + } + } + + private int get_Last_number(string path) + { + int outPut = 0; + var lstNrFile = Path.Combine(path, "lastNrFile.txt"); + if (File.Exists(lstNrFile)) + { + outPut = int.Parse(File.ReadAllText(lstNrFile)); + } + outPut++; + return outPut; + } + private void set_Last_number(string path, int lastNr) + { + var lstNrFile = Path.Combine(path, "lastNrFile.txt"); + var SavedNumber = lastNr.ToString(); + File.WriteAllText(lstNrFile, SavedNumber); + } } }