Click here to Skip to main content
15,918,742 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to move files from a Parent source folder to a destination folder in c#. In the parent source folder there would be a parent sub folder and with in it multiple sub folders and if any file exists in the sub folders those files must get archived with same structure as that of the parent source folder.

I tried to call a recursive method but archiving works for only a single parent sub folder.

What I have tried:

public void TEST(string source,string dest)
   {

       string tempsource = string.Empty;
       string tempdest = string.Empty;

       if (string.IsNullOrEmpty(dest)==false)
       {

           DirectoryInfo dirInfo = new DirectoryInfo(dest);
           if (dirInfo.Exists == false)
               Directory.CreateDirectory(dest);


           DirectoryInfo dir = new DirectoryInfo(source);
           DirectoryInfo[] dirs = dir.GetDirectories();

           int archivalagefield = 0;


           foreach (DirectoryInfo subdir in dirs)
           {
               tempsource = Path.Combine(source, subdir.Name);
               tempdest = Path.Combine(dest, subdir.Name);
               if (!Directory.Exists(tempdest))
                   try
                   {
                       //Directory.Move(subdir.FullName, temppath);
                       Directory.CreateDirectory(tempdest);
                       string[] files = Directory.GetFiles(tempsource);
                       GetArchiveLogDate GA = new GetArchiveLogDate();
                        DataSet dsGetArchiveAgeDate = GA.FetchMaxArchiveDateValue();
                        string ArchivalAge = dsGetArchiveAgeDate.Tables[1].Rows[0]["PARAMVALUE"].ToString().Trim();
                        if (dsGetArchiveAgeDate.Tables.Count > 0)
                        {
                            if (dsGetArchiveAgeDate.Tables[1].Rows.Count > 0)
                            {
                                archivalagefield = int.Parse(ArchivalAge) * -1;
                            }
                        }

                       foreach (string file in files)
                       {
                           try
                           {
                               DateTime FromDateTime = DateTime.Now.AddDays(ArchivalAgeField);
                               DateTime FromDate = Convert.ToDateTime(FromDateTime.ToString("yyyy/MM/dd"));
                               DateTime dtFileCreatedDate = File.GetCreationTime(file);


                               if (dtFileCreatedDate <= FromDate) // Only if the CreatedDate is less than FromDate, directory needs to be created and file needs to be moved
                               {
                                   string name = Path.GetFileName(file);
                                   string destFile = Path.Combine(tempdest, name);

                                   if (name != "file") File.Move(file, destFile);
                               }
                           }
                           catch
                           {

                           }
                       }

                   }
                   catch
                   {

                   }
           }
       }
       tempsource = Path.Combine(tempsource, appendedfolder);
       tempdest = Path.Combine(tempdest, appendedfolder);
       TEST(tempsource, tempdest);
   }
Posted
Updated 3-Nov-17 5:19am

1 solution

Do you need a pure C# solution for this? Because most folks would just use a utility like rsync (POSIX) or robocopy (Windows) since a lot of the mirroring/replication functionality is built into it. Just replicate the structure in your archive and move the files. I'm all for reinventing the wheel if this exercise is academic, but in the real world, you use a proven tool to perform its given task.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900