Big Pimpin
Big Pimpin

Reputation: 437

Move File To Sub-Directories

Moving files is easy & clear in .NET what gets me is how to move to a sub-directory For example, let's say I have a file in my source destination called Joe_2.mp3, and I need to move it to destination directory and to the most recently modified sub-folder if one exists. If it does not just move it straight into the destination directory. How would I 1st check for sub-directories & if they exist find the most recently modified one and move the source file their?

This is what I was using when it was just a straight move from directory1 to directory2

private string source = @"C:\\First\\";
private string unclear = @"C:\\Second\\";
public Form1()
{
  InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
  DirectoryInfo FirstCheck = new DirectoryInfo(@"C:\\First\\");
  DirectoryInfo SecondCheck = new DirectoryInfo(@"C:\\Second\\");
  IEnumerable<string> list1 = FirstCheck.GetFiles("*.*").Select(fi => fi.Name);
  IEnumerable<string> list2 = SecondCheck.EnumerateFiles("*", SearchOption.AllDirectories).Select(fi => fi.Name);
  IEnumerable<string> missing = list1.Except(list2);
  foreach (var file in missing)
  {
    var subdi = new DirectoryInfo(SecondCheck.ToString());
    var direcetories = subdi.EnumerateDirectories().OrderBy(d => d.CreationTime).Select(d => d.Name).ToList();
    File.Move(source, unclear);
  }
}

Upvotes: 0

Views: 68

Answers (1)

GWLlosa
GWLlosa

Reputation: 24443

Use Directory.GetDirectories to get the paths of all the subdirectories, then use the Directory.GetCreationTime to get the info you need. Then you can just sort them and take the minimal value that matches your criteria.

Upvotes: 3

Related Questions