Return to Snippet

Revision: 35566
at November 10, 2010 11:43 by Sazabi02


Initial Code
/// <summary>
        /// Generates a new path for duplicate filenames.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        private string GetNewPathForDupes( string path )
        {
            string directory = Path.GetDirectoryName( path );
            string filename = Path.GetFileNameWithoutExtension( path );
            string extension = Path.GetExtension( path );
            int counter = 1;

            string newFullPath;

            do
            {
                string newFilename = "{0}({1}).{2}".FormatWith( filename, counter, extension );
                newFullPath = Path.Combine( directory, newFilename );
                counter++;
            } while ( System.IO.File.Exists( newFullPath ) );

            return newFullPath;
        }

Initial URL


Initial Description
Modifies the file name in case an existing filename is in the specified path.
(Ex: myfile.dat
myfile(1).dat
myfile(2).dat
myfile(3).dat)

Initial Title
Generate new file name for duplicate files

Initial Tags
files

Initial Language
C#