none
Datein kopieren in vorgegebener Ordnerstrukur RRS feed

  • Frage

  • Hallo,

    folgende Anforderung:

    ich habe im Ordner "Eigene Bilder" auf der lokalen Festplatte folgende Ordnerstrukur:

    Eigene Bilder > Events > 2014 > Für jeden Ereignes ein Ordner mit unterschiedlichen Namen

    Eigene Bilder > EOS Archiv > 2014 > Animals

    Eigene Bilder > EOS Archiv > 2014 > Plants

    Eigene Bilder > EOS Archiv > 2014 > Portraits

    Eigene Bilder > EOS Archiv > 2014 > Misc

    Eigene Bilder > Projects

    Eigene Bilder > Publications

    Nun habe ich als Backup auf einer externen Festplatte die gleiche Ordnerstruktur. Ich wollte mir nun eine Anwendung erstellen mit der ich mit einem Klick alle Bilder von der lokalen Festplatte auf die externe Festplatte in die richtigen Ordner kopiere. Ich habe schon ein paar kleine sachen mit VS2012 und C# bewältigen können nun steh ich aber vor einer neuen Herausforderung und weis leider nicht weiter. Ich hoffe einer von euch kann mir helfen und würde mich über ein Lösungsansatz freuen. Anbei erstmal mein erstellter Code-Versuch. Mit freundlichen Gruß Andre

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    namespace PictureUpload
    {
        public partial class frm_Main : Form
        {
            public DirectoryInfo ExtDriveRoot, SrcEvents, DestEvents, SrcAnimals, DestAnimals, SrcMisc, DestMisc, SrcPlants, DestPlants, SrcPortraits, DestPortraits, SrcScapes, DestScapes, SrcStillLife, DestStillLife, SrcUrban, DestUrban, SrcProjects, DestProjects, SrcPublications, DestPublications;
            public List<string> PathList, DestPathList;
    
            public void GetDirectoryStatus() 
            {
                // \Event
                if (SrcEvents.Exists == true)
                {
                    lbl_StatusDirEvents.Text = "Ready";
                    lbl_StatusDirEvents.BackColor = Color.GreenYellow;
                    lbl_StatusDirEvents.ForeColor = Color.Black;
                    PathList.Add(SrcEvents.FullName);
                    DestPathList.Add(DestEvents.FullName);
                }
                else
                {
                    lbl_StatusDirEvents.Text = "Not found!";
                    lbl_StatusDirEvents.BackColor = Color.Gainsboro;
                    lbl_StatusDirEvents.ForeColor = Color.DimGray;
                    PathList.Remove(SrcEvents.FullName);
                    DestPathList.Remove(DestEvents.FullName);
                }
    
                // \EOS Archiv\Animals
                if (SrcAnimals.Exists == true)
                {
                    lbl_StatusDirAnimals.Text = "Ready";
                    lbl_StatusDirAnimals.BackColor = Color.GreenYellow;
                    lbl_StatusDirAnimals.ForeColor = Color.Black;
                    PathList.Add(SrcAnimals.FullName);
                    DestPathList.Add(DestAnimals.FullName);
                }
                else
                {
                    lbl_StatusDirAnimals.Text = "Not found!";
                    lbl_StatusDirAnimals.BackColor = Color.Gainsboro;
                    lbl_StatusDirAnimals.ForeColor = Color.DimGray;
                    PathList.Remove(SrcAnimals.FullName);
                    DestPathList.Remove(DestAnimals.FullName);
                }
    
                // \EOS Archiv\Misc
                if (SrcMisc.Exists == true)
                {
                    lbl_StatusDirMisc.Text = "Ready";
                    lbl_StatusDirMisc.BackColor = Color.GreenYellow;
                    lbl_StatusDirMisc.ForeColor = Color.Black;
                    PathList.Add(SrcMisc.FullName);
                    DestPathList.Add(DestMisc.FullName);
                }
                else
                {
                    lbl_StatusDirMisc.Text = "Not found!";
                    lbl_StatusDirMisc.BackColor = Color.Gainsboro;
                    lbl_StatusDirMisc.ForeColor = Color.DimGray;
                    PathList.Remove(SrcMisc.FullName);
                    DestPathList.Remove(DestMisc.FullName);
                }
    
                // \EOS Archiv\Plants
                if (SrcPlants.Exists == true)
                {
                    lbl_StatusDirPlants.Text = "Ready";
                    lbl_StatusDirPlants.BackColor = Color.GreenYellow;
                    lbl_StatusDirPlants.ForeColor = Color.Black;
                    PathList.Add(SrcPlants.FullName);
                    DestPathList.Add(DestPlants.FullName);
                }
                else
                {
                    lbl_StatusDirPlants.Text = "Not found!";
                    lbl_StatusDirPlants.BackColor = Color.Gainsboro;
                    lbl_StatusDirPlants.ForeColor = Color.DimGray;
                    PathList.Remove(SrcPlants.FullName);
                    DestPathList.Remove(DestPlants.FullName);
                }
    
                // \EOS Archiv\Portraits
                if (SrcPortraits.Exists == true)
                {
                    lbl_StatusDirPortraits.Text = "Ready";
                    lbl_StatusDirPortraits.BackColor = Color.GreenYellow;
                    lbl_StatusDirPortraits.ForeColor = Color.Black;
                    PathList.Add(SrcPortraits.FullName);
                    DestPathList.Add(DestPortraits.FullName);
                }
                else
                {
                    lbl_StatusDirPortraits.Text = "Not found!";
                    lbl_StatusDirPortraits.BackColor = Color.Gainsboro;
                    lbl_StatusDirPortraits.ForeColor = Color.DimGray;
                    PathList.Remove(SrcPortraits.FullName);
                    DestPathList.Remove(DestPortraits.FullName);
                }
    
                // \EOS Archiv\Scapes & Skies
                if (SrcScapes.Exists == true)
                {
                    lbl_StatusDirScapes.Text = "Ready";
                    lbl_StatusDirScapes.BackColor = Color.GreenYellow;
                    lbl_StatusDirScapes.ForeColor = Color.Black;
                    PathList.Add(SrcScapes.FullName);
                    DestPathList.Add(DestScapes.FullName);
                }
                else
                {
                    lbl_StatusDirScapes.Text = "Not found!";
                    lbl_StatusDirScapes.BackColor = Color.Gainsboro;
                    lbl_StatusDirScapes.ForeColor = Color.DimGray;
                    PathList.Remove(SrcScapes.FullName);
                    DestPathList.Remove(DestScapes.FullName);
                }
    
                // \EOS Archiv\Still Life
                if (SrcStillLife.Exists == true)
                {
                    lbl_StatusDirStillLife.Text = "Ready";
                    lbl_StatusDirStillLife.BackColor = Color.GreenYellow;
                    lbl_StatusDirStillLife.ForeColor = Color.Black;
                    PathList.Add(SrcStillLife.FullName);
                    DestPathList.Add(DestStillLife.FullName);
                }
                else
                {
                    lbl_StatusDirStillLife.Text = "Not found!";
                    lbl_StatusDirStillLife.BackColor = Color.Gainsboro;
                    lbl_StatusDirStillLife.ForeColor = Color.DimGray;
                    PathList.Remove(SrcStillLife.FullName);
                    DestPathList.Remove(DestStillLife.FullName);
                }
    
                // \EOS Archiv\Urban Exploring
                if (SrcUrban.Exists == true)
                {
                    lbl_StatusDirUrbanEx.Text = "Ready";
                    lbl_StatusDirUrbanEx.BackColor = Color.GreenYellow;
                    lbl_StatusDirUrbanEx.ForeColor = Color.Black;
                    PathList.Add(SrcUrban.FullName);
                    DestPathList.Add(DestUrban.FullName);
                }
                else
                {
                    lbl_StatusDirUrbanEx.Text = "Not found!";
                    lbl_StatusDirUrbanEx.BackColor = Color.Gainsboro;
                    lbl_StatusDirUrbanEx.ForeColor = Color.DimGray;
                    PathList.Remove(SrcUrban.FullName);
                    DestPathList.Remove(DestUrban.FullName);
                }
    
                // \Projects
                if (SrcProjects.Exists == true)
                {
                    lbl_StatusDirProjects.Text = "Ready";
                    lbl_StatusDirProjects.BackColor = Color.GreenYellow;
                    lbl_StatusDirProjects.ForeColor = Color.Black;
                    PathList.Add(SrcProjects.FullName);
                    DestPathList.Add(DestProjects.FullName);
                }
                else
                {
                    lbl_StatusDirProjects.Text = "Not found!";
                    lbl_StatusDirProjects.BackColor = Color.Gainsboro;
                    lbl_StatusDirProjects.ForeColor = Color.DimGray;
                    PathList.Remove(SrcProjects.FullName);
                    DestPathList.Remove(DestProjects.FullName);
                }
    
                // \Publications
                if (SrcPublications.Exists == true)
                {
                    lbl_StatusDirPublications.Text = "Ready";
                    lbl_StatusDirPublications.BackColor = Color.GreenYellow;
                    lbl_StatusDirPublications.ForeColor = Color.Black;
                    PathList.Add(SrcPublications.FullName);
                    DestPathList.Add(DestPublications.FullName);
                }
                else
                {
                    lbl_StatusDirPublications.Text = "Not found!";
                    lbl_StatusDirPublications.BackColor = Color.Gainsboro;
                    lbl_StatusDirPublications.ForeColor = Color.DimGray;
                    PathList.Remove(SrcPublications.FullName);
                    DestPathList.Remove(DestPublications.FullName);
                }
    
                lbx_Directorys.DataSource = PathList;
    
            }
    
            public frm_Main()
            {
                InitializeComponent();
    
                ExtDriveRoot = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath));
    
                // \Events
                SrcEvents = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Events\\" + DateTime.Now.Year.ToString());
                DestEvents = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\Events\\" + DateTime.Now.Year.ToString());
    
                // \EOS Archiv\Animals
                SrcAnimals = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\EOS Archiv\\Animals\\" + DateTime.Now.Year.ToString());
                DestAnimals = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\EOS Archiv\\Animals\\" + DateTime.Now.Year.ToString());
    
                // \EOS Archiv\Misc
                SrcMisc = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\EOS Archiv\\Misc\\" + DateTime.Now.Year.ToString());
                DestMisc = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\EOS Archiv\\Misc\\" + DateTime.Now.Year.ToString());
    
                // \EOS Archiv\Plants
                SrcPlants = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\EOS Archiv\\Plants\\" + DateTime.Now.Year.ToString());
                DestPlants = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\EOS Archiv\\Plants\\" + DateTime.Now.Year.ToString());
    
                // \EOS Archiv\Portraits
                SrcPortraits = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\EOS Archiv\\Portraits\\" + DateTime.Now.Year.ToString());
                DestPortraits = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\EOS Archiv\\Portraits\\" + DateTime.Now.Year.ToString());
    
                // \EOS Archiv\Scapes & Skies
                SrcScapes = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\EOS Archiv\\Scapes & Skies\\" + DateTime.Now.Year.ToString());
                DestScapes = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\EOS Archiv\\Scapes & Skies\\" + DateTime.Now.Year.ToString());
    
                // \EOS Archiv\Still Life
                SrcStillLife = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\EOS Archiv\\Still Life\\" + DateTime.Now.Year.ToString());
                DestStillLife = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\EOS Archiv\\Still Life\\" + DateTime.Now.Year.ToString());
    
                // \EOS Archiv\Urban Exploring
                SrcUrban = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\EOS Archiv\\Urban Exploring\\" + DateTime.Now.Year.ToString());
                DestUrban = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\EOS Archiv\\Urban Exploring\\" + DateTime.Now.Year.ToString());
    
                // \Projects
                SrcProjects = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Projects\\" + DateTime.Now.Year.ToString());
                DestProjects = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\Projects\\" + DateTime.Now.Year.ToString());
    
                // \Publications
                SrcPublications = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\Publications\\" + DateTime.Now.Year.ToString());
                DestPublications = new DirectoryInfo(Directory.GetDirectoryRoot(Application.StartupPath) + "\\Publications\\" + DateTime.Now.Year.ToString());
    
                PathList = new List<string>();
            }
    
            private void frm_Main_Load(object sender, EventArgs e)
            {
                //Lese Laufwerksbuchstaben der externen Festplatte aus.
                lbl_BURootLetter.Text = "Drive letter: " + ExtDriveRoot.FullName;
    
                //Ordnerstatus überprüfen
                GetDirectoryStatus();
            }
    
            private void btn_Upload_Click(object sender, EventArgs e)
            {
                foreach (string s in PathList)
                {
                    DirectoryInfo di = new DirectoryInfo(s);
                    FileInfo[] files = di.GetFiles();
                    foreach (FileInfo file in files)
                    {
                        string Destination = Path.Combine(s, file.Name);
                        file.CopyTo(Destination, false);
                    }
                }
            }
        }
    }
    

    Donnerstag, 19. Dezember 2013 15:04

Antworten

  • Hallo,
    Ich nehme mal an, das du alle Dateien bzw. alle Dateien von einem Typ kopieren willst. Dazu solltest du dir den Aufbau der Pfade zu den Ordnern nochmal genauer ansehen:
    C:\User\Tom\Bilder\Projects\...
    D:\Backups\Bilder\Projects\...
    Die blauen Pfadteile sind identisch. Also reicht es aus, wenn du den Ordner mit den enthaltenen Dateien kopierst. Ich habe mal eine kleine Methode dazu geschrieben:

    static void CopyFiles(string folder, string destination, string pattern, bool overwrite)
    {
      if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);//Zielordner existierte noch nicht
      foreach (string file in Directory.GetFiles(folder, pattern))//Dateien auflisten die dem Suchkriterium entsprechen
      {
        File.Copy(file, Path.Combine(destination, new FileInfo(file).Name), overwrite);//Kopiert die Datei
      }
      foreach (string subfolder in Directory.GetDirectories(folder).Select(x => new DirectoryInfo(x).Name))//Unterordner auflisten, Select wählt den Ordnernamen ohne Pfad aus
      {
        CopyFiles(Path.Combine(folder, subfolder), Path.Combine(destination, subfolder), pattern, overwrite);//Rekursiv aufrufen um Unterordner zu kopieren
      }
    }

    Dabei musst du nur den Pfad zum zu kopierenden Ordner und den Zielordner angeben. Der 3. Parameter gibt das Suchmuster an. Der Letzte Parameter bestimmt ob existierende Dateien überschrieben werden sollen.
    Um nun Exceptions wegen fehlenden Rechten etc zu verhindern, musst du das File.Copy in einen Try...Catch-Block packen und die Exception ignorieren. Die Select-Methode ist erst ab .NET 3.5 verfügbar.
    Der Ziel-Ordner wird automatisch erstellt, falls nicht vorhanden. Mögliche Suchmuster wären beispielsweise: *.* für alle Dateien (auch ohne Endungen) und *.jpg für alle JPG-Dateien.

    Noch eine kleine Anmerkung zu deinem Code: Pfade solltest du mit Path.Combine verknüpfen. Dort werden die \ von alleine hinzugefügt, wenn diese nicht vorhanden sein sollten.


    Koopakiller [kuːpakɪllɐ] (Tom Lambert)
    Webseite | Code Beispiele | Facebook | Twitter | Snippets   C# ↔ VB.NET Konverter
    Markiert bitte beantwortende Posts als Antwort und bewertet Beiträge. Danke.

    • Als Antwort markiert Andre Aper Donnerstag, 19. Dezember 2013 19:43
    Donnerstag, 19. Dezember 2013 15:33
    Moderator

Alle Antworten

  • Hallo,
    Ich nehme mal an, das du alle Dateien bzw. alle Dateien von einem Typ kopieren willst. Dazu solltest du dir den Aufbau der Pfade zu den Ordnern nochmal genauer ansehen:
    C:\User\Tom\Bilder\Projects\...
    D:\Backups\Bilder\Projects\...
    Die blauen Pfadteile sind identisch. Also reicht es aus, wenn du den Ordner mit den enthaltenen Dateien kopierst. Ich habe mal eine kleine Methode dazu geschrieben:

    static void CopyFiles(string folder, string destination, string pattern, bool overwrite)
    {
      if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);//Zielordner existierte noch nicht
      foreach (string file in Directory.GetFiles(folder, pattern))//Dateien auflisten die dem Suchkriterium entsprechen
      {
        File.Copy(file, Path.Combine(destination, new FileInfo(file).Name), overwrite);//Kopiert die Datei
      }
      foreach (string subfolder in Directory.GetDirectories(folder).Select(x => new DirectoryInfo(x).Name))//Unterordner auflisten, Select wählt den Ordnernamen ohne Pfad aus
      {
        CopyFiles(Path.Combine(folder, subfolder), Path.Combine(destination, subfolder), pattern, overwrite);//Rekursiv aufrufen um Unterordner zu kopieren
      }
    }

    Dabei musst du nur den Pfad zum zu kopierenden Ordner und den Zielordner angeben. Der 3. Parameter gibt das Suchmuster an. Der Letzte Parameter bestimmt ob existierende Dateien überschrieben werden sollen.
    Um nun Exceptions wegen fehlenden Rechten etc zu verhindern, musst du das File.Copy in einen Try...Catch-Block packen und die Exception ignorieren. Die Select-Methode ist erst ab .NET 3.5 verfügbar.
    Der Ziel-Ordner wird automatisch erstellt, falls nicht vorhanden. Mögliche Suchmuster wären beispielsweise: *.* für alle Dateien (auch ohne Endungen) und *.jpg für alle JPG-Dateien.

    Noch eine kleine Anmerkung zu deinem Code: Pfade solltest du mit Path.Combine verknüpfen. Dort werden die \ von alleine hinzugefügt, wenn diese nicht vorhanden sein sollten.


    Koopakiller [kuːpakɪllɐ] (Tom Lambert)
    Webseite | Code Beispiele | Facebook | Twitter | Snippets   C# ↔ VB.NET Konverter
    Markiert bitte beantwortende Posts als Antwort und bewertet Beiträge. Danke.

    • Als Antwort markiert Andre Aper Donnerstag, 19. Dezember 2013 19:43
    Donnerstag, 19. Dezember 2013 15:33
    Moderator
  • Wahnsinn... das ich da nicht selber drauf gekommen bin.

    Vielen vielen Dank.

    PS.: Den Tipp mit Path.Combine werde ich mir zu Herzen nehmen und mir mal genauer anschauen.

    Auch hierfür ein dickes Danke

    Donnerstag, 19. Dezember 2013 19:52