1

Is there a more efficient way to rename a file in Windows other than:

System.IO.File.Move(sourceFileName, destFileName);  

I have millions of them and using the above will take almost a week to run.

Mitchell
  • 169
  • 1
  • 14
  • Is there any pattern for the file rename ? – Raptor Jul 14 '14 at 03:44
  • "I have millions of them and using the above will take almost a week to run" - that implies a single rename will take approx. 0.0672 seconds (based on 1 week and 9 million files) – Mitch Wheat Jul 14 '14 at 03:46
  • @Raptor yes it changes depending on criteria in a text file. The program reads a line in the file, looks at the text and changes the name depending on what's on the line. – Mitchell Jul 14 '14 at 03:51
  • @MitchWheat What's your point? I have more than that. – Mitchell Jul 14 '14 at 03:52

1 Answers1

1

File.Move is a thin wrapper around the Win32 API. I doubt there is any faster way short of directly modifying the raw data at the block level on the disk. I think you are out of luck looking for a faster way from managed code.

File.Move decompiled source:

public static void Move(string sourceFileName, string destFileName)
{
    if ((sourceFileName == null) || (destFileName == null))
    {
        throw new ArgumentNullException((sourceFileName == null) ? "sourceFileName" : "destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
    }
    if ((sourceFileName.Length == 0) || (destFileName.Length == 0))
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), (sourceFileName.Length == 0) ? "sourceFileName" : "destFileName");
    }
    string fullPathInternal = Path.GetFullPathInternal(sourceFileName);
    new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand();
    string dst = Path.GetFullPathInternal(destFileName);
    new FileIOPermission(FileIOPermissionAccess.Write, new string[] { dst }, false, false).Demand();
    if (!InternalExists(fullPathInternal))
    {
        __Error.WinIOError(2, fullPathInternal);
    }
    if (!Win32Native.MoveFile(fullPathInternal, dst))
    {
        __Error.WinIOError();
    }
}
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76