Laden...

Ordner(struktur) und Dateien kopieren mit In/Exclude

Erstellt von mosspower vor 15 Jahren Letzter Beitrag vor 14 Jahren 9.047 Views
mosspower Themenstarter:in
456 Beiträge seit 2007
vor 15 Jahren
Ordner(struktur) und Dateien kopieren mit In/Exclude

Beschreibung: Ordner(struktur) und Dateien kopieren

Mit dem angehängten Code kann eine ganze Ordnerstruktur (also inklusive Unterverzeichnis) kopiert werden. Zusätzlich kann noch mit regulären Ausdrücken Files / Ordner inkludiert, bzw. excludiert werden.


 /// <summary>
    /// <see cref="IOUtil.CopyDirectory(String, String, String, String)"/>
    /// </summary>
    public static void CopyDirectory(String rootDest, String rootSource) {
      IOUtil.CopyDirectory(rootDest, rootSource, null, null);
    }

    /// <summary>
    /// Copies a directory and the directory graph
    /// with all files. Replaces the files in the
    /// new directory, if existing
    /// </summary>
    /// <param name="rootSource">The qualified root source directory</param>
    /// <param name="rootDest">The qualified root destination directory</param>
    /// <param name="includePattern">The include regex pattern for including files and folders</param>
    /// <param name="excludePattern">The exclude regex pattern for excluding files
    /// and folders. Excludes goes for include, this means, if include and exclude is matching, the the file
    /// or folder will not be copied</param>
    /// <exception cref="ArgumentException">Handles all empty or null values for the passed parameters</exception>
    /// <exception cref="IOException">Handles all file and directory operation exceptions</exception>
    public static void CopyDirectory(String rootSource, String rootDest, String includePattern, String excludePattern) {

      try {
        if(ValueUtil.IsEmptyOrDefault(rootDest, rootSource)) {
          throw new ArgumentException("Parameter rootDest and / or root Source was passed null or empty");
        }

        DirectoryInfo directoryInfo = new DirectoryInfo(rootSource);

        // Create destination directory
        if(!Directory.Exists(rootDest)) {
          Directory.CreateDirectory(rootDest);
        }

        // Get all files in the current directory
        FileInfo[] files = directoryInfo.GetFiles();
        Regex regexInclude = null;
        Regex regexExclude = null;

        if(ValueUtil.IsNotEmptyOrDefault(includePattern)) {
          RegexUtil.Validate(includePattern);
          regexInclude = new Regex(includePattern);
        }

        if(ValueUtil.IsNotEmptyOrDefault(excludePattern)) {
          RegexUtil.Validate(excludePattern);
          regexExclude = new Regex(excludePattern);
        }

        // Iterate all files
        foreach(FileInfo file in files) {
          bool copy = true;

          if(regexInclude != null) {
            if(!regexInclude.IsMatch(file.Name)) {
              copy = false;
            }
          }

          if(regexExclude != null) {
            if(regexExclude.IsMatch(file.Name)) {
              copy = false;
            }
          }

          if(copy) {
            // Copy the file to the destination
            file.CopyTo(Path.Combine(rootDest, file.Name), true);
          }
        }

        // Iterate all directories in the current directory
        // and use recursion
        DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();

        foreach(DirectoryInfo dirSub in directoryInfos) {
          bool copy = true;

          if(regexInclude != null) {
            if(!regexInclude.IsMatch(dirSub.Name)) {
              copy = false;
            }
          }

          if(regexExclude != null) {
            if(regexExclude.IsMatch(dirSub.Name)) {
              copy = false;
            }
          }

          if(copy) {
            // Copy directory
            IOUtil.CopyDirectory(dirSub.FullName,
              Path.Combine(rootDest, dirSub.Name), includePattern, excludePattern);
          }
        }
      }
      catch(ArgumentException) {
        throw;
      }
      catch(Exception ex) {
        throw new IOException(ex.Message, ex);
      }
    }

Schlagwörter: Directory copy Ordner kopieren

Gelöschter Account
vor 15 Jahren

Ohne deine Idee oder deine Anstrengunggen torpedieren zu wollen ! siehe Microsoft Sync Framework Stichwort: MSDN: Sync Services for File Systems 😁

5.299 Beiträge seit 2008
vor 15 Jahren

hi!

Mosspower hat da eine rekursive Methode geschrieben, mit der man einen Ordner komplett umkopieren kann, in etwa:

IOUtil.CopyDirectory(@"C:\Programming\BlablaNeu", @"C:\Programming\Blabla");

Dafür würde ich doch kein zusätzliches Framework installieren!
V.a. wo ich kein Deut kapiere, wie ein entsprechender Aufruf mithilfe des Sync Frameworks funktionieren würde - Beispiel?

Der frühe Apfel fängt den Wurm.

Gelöschter Account
vor 15 Jahren

siehe innerhalb des syncFrameworks den FileSyncprovider.

Vorteile:
-komplexe kolisions behandlung
-vollständige fehlerbehandlung von io operationen (permission ect.)
-regex filter
-komplexe kopier szenarien

F
10.010 Beiträge seit 2004
vor 15 Jahren

@ErfinderDesRades:
Das zusätzliche FW besteht aus einer DLL.
Und diese kann Dir gerade im DB Umfeld ( aber wie hier gesehen auch anderswo )
so viel arbeit abnehmen, das du es wirklich mal versuchen solltest.

F
100 Beiträge seit 2009
vor 14 Jahren

vielleicht etwas spät, aber

public static void CopyDirectory(String rootDest, String rootSource) {
      IOUtil.CopyDirectory(rootDest, rootSource, null, null);
    }

public static void CopyDirectory(String rootSource, String rootDest, String includePattern, String excludePattern) {

die beiden stimmen nicht ganz überein, das müsste doch so sein:

public static void CopyDirectory(String rootSource, String rootDest){
      IOUtil.CopyDirectory(rootSource, rootDest, null, null);
    }

public static void CopyDirectory(String rootSource, String rootDest, String includePattern, String excludePattern) {

und die anderen Utils konnte ich auch nicht finden:
ValueUtil, RegexUtil

3.971 Beiträge seit 2006
vor 14 Jahren

Was man sich auch mal anschauen kann, ist der Copy-Task sowie die Klasse FileSet von NAnt.

Es gibt 3 Arten von Menschen, die die bis 3 zählen können und die, die es nicht können...