title: File.Move() rename not working
body: <p>My app takes "unclean" file names and "cleans" them up.  "Unclean" file names contain characters like @, #, ~, +, %, etc.  The "cleaning" process replaces those chars with "".  However, I found that if there are two files in the same folder that, after a cleaning, will have the same name, my app does not rename either file.  (I.e. ##test.txt and ~test.txt will both be named test.txt after the cleaning).  </p>
  
  <p>Therefore, I put in a loop that basically checks to see if the file name my app is trying to rename already exists in the folder.  However, I tried running this and it would not rename all the files.  Am I doing something wrong?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>        public void FileCleanup(List&lt;string&gt; paths)
      {
          string regPattern = (@"[~#&amp;!%+{}]+");
          string replacement = "";
  
          Regex regExPattern = new Regex(regPattern);
          List&lt;string&gt; existingNames = new List&lt;string&gt;();
          StreamWriter errors = new StreamWriter(@"C:\Documents and Settings\joe.schmoe\Desktop\SharePointTesting\Errors.txt");
          StreamWriter resultsofRename = new StreamWriter(@"C:\Documents and Settings\joe.schmoe\Desktop\SharePointTesting\Results of File Rename.txt");
  
          var filesCount = new Dictionary&lt;string, int&gt;();
          string replaceSpecialCharsWith = "_";
  
          foreach (string files2 in paths)
  
              try
              {
                  string filenameOnly = Path.GetFileName(files2);
                  string pathOnly = Path.GetDirectoryName(files2);
                  string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
                  string sanitized = Path.Combine(pathOnly, sanitizedFileName);
                  if (!System.IO.File.Exists(sanitized))
                  {
                      System.IO.File.Move(files2, sanitized);
                      resultsofRename.Write("Path: " + pathOnly + " / " + "Old File Name: " + filenameOnly + "New File Name: " + sanitized + "\r\n" + "\r\n");   
                  }
  
                  else 
                  {
                      existingNames.Add(sanitized);
                      foreach (string names in existingNames)
                      {
  
                          string sanitizedPath = regExPattern.Replace(names, replaceSpecialCharsWith);
                          if (filesCount.ContainsKey(sanitizedPath))
                          {
                              filesCount[names]++;
                          }
                          else
                          {
                              filesCount.Add(sanitizedPath, 1);
                          }
  
                          string newFileName = String.Format("{0},{1}, {2}", Path.GetFileNameWithoutExtension(sanitizedPath),
                          filesCount[sanitizedPath] != 0
                          ? filesCount[sanitizedPath].ToString()
                          : "",
                          Path.GetExtension(sanitizedPath));
  
                          string newFilePath = Path.Combine(Path.GetDirectoryName(sanitizedPath), newFileName);
                          System.IO.File.Move(names, newFileName);
  
                      }
                   }
                }
              catch (Exception e)
              {
                  //write to streamwriter
  
              }
  
      }
  
  }
  </code></pre>
  
  <p>Anybody have ANY idea why my code won't rename duplicate files uniquely?</p>