user3477335
user3477335

Reputation: 165

Identify the new folders and files copied into a source directory c#

I have few folders inside directory which I need to process and move the files into another directory with same folder structure of source directory.

Source Directory(Dir):

- folder1 *subfolder1 *file 1 *file 2 *subfolder2 *file 3 *file 4 -folder 2 *subfolder3 *file 5 *file 6 *subfolder4 *file 7 *file 8">

So here I need to process source directory and move each subfolder to destination once I process all the files inside subfolder. So destination should have same directory structure as source directory once it processed all the folders from source directory.

So, I have implemented all the above logic but here folders are copying continuously into source dir. So, I need to process those folders too and move to destination.

So, I need to load the source dir again and again to identify if there any new folders to process. I have also need to skip any old folders which I processed in previous loading of source dir.

Below is my logic which am trying to implement:

while (i == 0)//loading the source dir again and again to check the new folders
        {  
        DirectoryInfo sourcefolder = new DirectoryInfo(/* path of soure dir */);
        DirectoryInfo[] sourceRreportSubfolders = sourcefolder.GetDirectories();

            foreach (DirectoryInfo dir in sourceRreportSubfolders)
            {
                objFile.RecursiveFiles(dir.FullName); // method to process the folders
            }
        }

I need to implement follwoing logic:

  1. Skip the previous folders which I processed
  2. Identify new folders to process
  3. If its last folders check the last folder whether it's processed all the files inside the last folder. If yes then load the source dir to check for any new folders again and again. If no, process those files.

Upvotes: 0

Views: 381

Answers (1)

Raja Nadar
Raja Nadar

Reputation: 9489

declare a variable like:

List<string> copiedSourceFolders = new List<string>();

whenever you traverse a source folder/sub-folder and copy all its files, add the folder path to this list.

copiedSourceFolders.Add(processedSourceDir);

whenever you traverse a source folder/sub-folder, check the presence of the path in this list before going ahead with copying of the files.

if (!copiedSourceFolders.Contains(processedSourceDir))
{
 // COPY STUFF.
}

Upvotes: 0

Related Questions