C Copies files to specified folders and organizes them

  • 2021-06-29 11:51:00
  • OfStack

Here is a simple example of copying (cutting is copying and deleting) files to a specified path in C# and archiving them by date.Two points to note are:

1) The path to the file is the key, using the double slash in the program\\

2) Differences between files and folders


private void DoWork()
{
   String dir="D:\\ABC"
   // Create backup folder named by time   
   String bakDir = dir + "\\bak\\" + DateTime.Now.ToString("yyyy-MM-dd");
 
   if (Directory.Exists(bakDir) == false){
       Directory.CreateDirectory(bakDir);
   }
   string[] files = Directory.GetFiles(dir);
   if (files.Length != 0) {
      foreach (string file in files) {
      FileInfo fileinfo = new FileInfo(file);
      try{
        string fileName = file.Replace(dir, "");
        // Backup Files 
        File.Copy(file,Path.Combine(bakDir,fileName));
        File.Delete(file);
      }  
   }
}

Attach the implementation of other netizens:


private void CopyDir(string srcPath, string aimPath)
   {
 
   try
 
   {
 
   //  Check that the target directory ends with a directory split character or adds if not 
 
   if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
 
   {
 
   aimPath += System.IO.Path.DirectorySeparatorChar;
 
   }
 
   //  Determine if the target directory exists or if it does not exist, create a new one 
 
   if (!System.IO.Directory.Exists(aimPath))
   {
 
   System.IO.Directory.CreateDirectory(aimPath);
 
   }
 
   //  Get a list of files in the source directory, which contains files and directory paths 1 Arrays 
 
   //  If you point at copy Use the following method for files below the target file without a directory 
 
   // string[] fileList = Directory.GetFiles ( srcPath ; 
 
   string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
 
   //  Traverse all files and directories 
 
   foreach (string file in fileList)
 
   {
 
   //  Processing as a directory first If this directory exists recursively Copy Files under this directory 
 
   if(System.IO.Directory.Exists(file))
 
   {
 
   CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
 
   }
 
   //  Otherwise direct Copy file 
 
   else
 
   {
 
   System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file),true);
 
   }
 
   }
 
   }
 
   catch(Exception e)
   {
 
   throw;
 
   }
 
   }
  }

Completely without recursive implementation, use of breadth-first algorithm can save stack space


using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
string sourcepath=@"C:\sourcedir";
Queue<FileSystemInfo> copyfolders = new Queue<FileSystemInfo>(new DirectoryInfo(sourcepath).GetFileSystemInfos());
string copytopath = @"C:\targetdir";
copytopath = (copytopath.LastIndexOf(Path.DirectorySeparatorChar) == copytopath.Length - 1) ? copytopath : (copytopath+Path.DirectorySeparatorChar) + Path.GetFileName(sourcepath);
Directory.CreateDirectory(copytopath);
while (copyfolders.Count>0)
{
  FileSystemInfo atom = opyfolders.Dequeue();
  FileInfo file = atom as FileInfo;
  if (file == null)
  {
    DirectoryInfo directory = atom as DirectoryInfo;
    Directory.CreateDirectory(directory.FullName.Replace(sourcepath,copytopath));
    foreach (FileSystemInfo fi in directory.GetFileSystemInfos())
      copyfolders.Enqueue(fi);
  }
  else
    file.CopyTo(file.FullName.Replace(sourcepath,copytopath));
}

The above is the whole content of this article, I hope you like it.


Related articles: