title: Concatenating folder names to file name and copy file to a folde
body: <p>My folder structure is as .. temp\2016\09\11\16</p>
  <p>In the last folder I have multiple text/json files.</p>
  <p>I would like to run a console application that would loop through every folder and rename the files with foldernames concatenated.</p>
  <p>So a file abcd.json would become 2016091116_abcd.json</p>
  <p>Then copy this file to another folder.</p>
  <p>I tried..</p>
  <pre><code>using System;
  using System.IO;
  using System.Collections;
  
  namespace concatdf
  {
      class Program
      {
          static void Main(string[] args)
          {
              string folderPath = &quot;D:\\500GBNP\\Projects\\temp&quot;;
  
              DirectoryInfo startDir = new DirectoryInfo(folderPath);
  
              RecurseFileStructure recurseFileStructure = new RecurseFileStructure();
              recurseFileStructure.TraverseDirectory(startDir);
          }
  
          public class RecurseFileStructure
          {
              public void TraverseDirectory(DirectoryInfo directoryInfo)
              {
                  var subdirectories = directoryInfo.EnumerateDirectories();
  
                  foreach (var subdirectory in subdirectories)
                  {
                      TraverseDirectory(subdirectory);
                  }
  
                  var files = directoryInfo.EnumerateFiles();
  
                  foreach (var file in files)
                  {
                      HandleFile(file);
                  }
              }
  
              void HandleFile(FileInfo file)
              {
                  string destfolderPath = &quot;D:\\500GBNP\\Projects\\temp1\\&quot;;
                 file.CopyTo(destfolderPath+file.Name);
              }
          }
      }
  }
  </code></pre>
  <p>With the above code I'm able to traverse and copy all files to target directory but file names do not get concatenated with foldernames.</p>
  <p>So a file abcd.json in folder temp\2016\09\11\16 would become 2016091116_abcd.json and all files get copied to temp1 folder.</p>
  <p>I would sincerely appreciate if someone could help.</p>