title: SSIS renaming files in a directory using script task
body: <p>I am using the below code, to rename all the files in a folder with a timestamp.
  But it is throwing an exception.</p>
  
  <p>Please suggest any solutions.</p>
  
  <pre><code>using System.IO;
  
  public void Main()
  {
      DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\\Users\\AShubhankar\\Desktop\\archive_feb");
      //if the director exists then proceed
      if (directoryInfo.Exists)
      {
          var fileList = directoryInfo.GetFiles();
  
          foreach (FileInfo fleInfo in fileList)
          {
              var newFileName = GetNewFileName(fleInfo);
              //copies the new file names
              fleInfo.CopyTo(newFileName, true);
              //delete the old/original files
              fleInfo.Delete();
          }
          Dts.TaskResult = (int)ScriptResults.Success;
      }
      else
      {
          MessageBox.Show("Directory not found");
          Dts.TaskResult = (int)ScriptResults.Failure;
      }
  }
  
  // Function to get the new file name        
  private static string GetNewFileName(FileInfo fleInfo)
  {
      var shortDate = DateTime.Now.ToShortDateString().Replace("/", string.Empty);
      var format = string.Format("{0}_{1}", shortDate);
      var extension = ".txt";
      return Path.Combine(fleInfo.DirectoryName, 
      string.Concat(fleInfo.Name.Split('.')[0], "_", format, extension));
  }
  </code></pre>
  
  <p><em>Exception thrown:</em></p>
  
  <p><a href="https://i.stack.imgur.com/xKyLw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xKyLw.png" alt="Image of the error description"></a></p>