1
Developer's Corner / Re: C# Rename file with incrementing number - file (1).ext
« on: February 02, 2016, 11:50 AM »
This version works pretty good too.
Code: C# [Select]
- private string GetNextFileName(string fileName) {
- string extension = Path.GetExtension(fileName);
- string pathName = Path.GetDirectoryName(fileName);
- string fileNameOnly = Path.Combine(pathName, Path.GetFileNameWithoutExtension(fileName));
- int i = 0;
- // If the file exists, keep trying until it doesn't
- while (File.Exists(fileName)) {
- i += 1;
- fileName = string.Format("{0}({1}){2}", fileNameOnly, i, extension);
- }
- return fileName;
- }