Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi,

I am trying to rename a file, if it already exists.

Here is my code:

while (File.Exists(destFile))
{
destFile = destFile.Remove(destFile.Length - 4);

n += 1;
destFile = string.Format("{0}({1}){2}", destFile, n, ".csv");
}

I am expecting the output like:

file.csv, file(1).csv, file(2).csv...

but I am getting the output :
file.csv, file(1).csv, file(1)(2).csv, file(1)(2)(3).csv...

How to fix this one?

Any help would be much appreciated.

What I have tried:

C#
while (File.Exists(destFile))
 {
   destFile = destFile.Remove(destFile.Length - 4);
                              
   n += 1;
   destFile = string.Format("{0}({1}){2}", destFile, n, ".csv");
  }
Posted
Updated 16-Nov-16 3:06am

Yes because you are using the same variable (destfile) each time round your loop.
Iteration 1: destfile = file.csv
Iteration 2: destfile = file(1).csv
Iteration 3: destfile = file(1)(2).csv

You need to parse out any (n) parts of the file name before adding the next one.
 
Share this answer
 
Comments
Member 10714689 16-Nov-16 5:39am    
thanks for the quick response. Is there any quick way to parse out (n) parts in c#?
Richard MacCutchan 16-Nov-16 5:44am    
You would probably be better using a regular expression. See Regex Class (System.Text.RegularExpressions)[^].
You are removing only the ".csv" extension. So if your file name is "file(1).csv", it will become "file(1)" + "(2)" + ".csv" = "file(1)(2).csv".

To avoid this, you must split the name into plain name, clasped count, and extension. Then use the incremented count to build the new name.

Possible solutions are using a regular expression, or locating the open and closing parentheses, getting the number in between and removing that part.

Another solution would be getting the plain name outside the loop and using that to build the name:
C#
if (File.Exists(destFile))
{
    int n = 0;
    string plainName = destFile.Remove(destFile.Length - 4);
    string newName;
    do
    {
        n++;
        newName = string.Format("{0}({1}){2}", plainName, n, ".csv");
    }
    while (File.Exists(newName));
}
 
Share this answer
 
Comments
#realJSOP 16-Nov-16 6:02am    
I would use various File.IO.Path.Getxxx methods to break the filename apart instead of hard-coding ".csv", but other than that, this is the answer.
finally solved this issue.

Here is my code:

using System.IO; // include this namespace

int n = 0;
while (File.Exists(destFile))
{
destFile = destFile.Remove(destFile.Length - 4);
if (destFile.Contains('('))
{
int underscoreIndex = destFile.LastIndexOf("(");
destFile = destFile.Substring(0, underscoreIndex);
}
n += 1;
destFile = string.Format("{0}({1}){2}", destFile, n, ".csv");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900