Mk0.Software.ImageSorter/Mk0.Software.ImageSorter/Dateirenamer.cs

92 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mk0.Software.ImageSorter
{
public partial class Dateirenamer : Form
{
private string quellPath;
public Dateirenamer(string quellPath)
{
InitializeComponent();
this.quellPath = quellPath;
}
private void Dateirenamer_Shown(object sender, EventArgs e)
{
if (!Directory.Exists(quellPath))
{
MessageBox.Show("Es wurde kein gültiger Quellpfad definiert. Bitte zuerst definieren, dann erneut versuchen.", "Kein Quellpfad definiert", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
private void ButtonAusfuehren_Click(object sender, EventArgs e)
{
if (CheckInput())
{
textBoxLog.Clear();
Scanner(quellPath, textBoxSuche.Text, textBoxErsetze.Text, checkBoxRekursiv.Checked);
}
}
private void ButtonSpeichern_Click(object sender, EventArgs e)
{
if (CheckInput())
{
MessageBox.Show("ok");
//todo
}
}
private bool CheckInput()
{
if (string.IsNullOrEmpty(textBoxSuche.Text))
{
return false;
}
return true;
}
public void Scanner(string pfad, string suchen, string ersetzen, bool rekursiv = false)
{
DirectoryInfo dir = new DirectoryInfo(pfad);
foreach (FileInfo file in dir.GetFiles("*.*"))
{
if (file.Name.Contains(suchen))
{
try
{
string newName = file.Name.Replace(suchen, ersetzen);
textBoxLog.AppendText("RENAME: " + dir.Name + "| " + file.Name + " -> " + newName + Environment.NewLine);
File.Move(Path.Combine(dir.FullName, file.Name), Path.Combine(dir.FullName, newName));
}
catch (Exception)
{
textBoxLog.AppendText("DELETE: " + dir.Name + "| " + file.Name + Environment.NewLine);
File.Delete(Path.Combine(dir.FullName, file.Name));
}
}
}
if (rekursiv)
{
foreach (DirectoryInfo dir2 in dir.GetDirectories())
{
Scanner(dir2.FullName, suchen, ersetzen, true);
}
}
}
}
}