使用C#自动重命名文件(如果已存在) [英] Automatically rename a file if it already exist using C#

查看:264
本文介绍了使用C#自动重命名文件(如果已存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试重命名文件,如果它已经存在。



这是我的代码:



while(File.Exists(destFile))

{

destFile = destFile .Remove(destFile.Length - 4);



n + = 1;

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

}



我期待输出如下:



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



但是我得到了输出:

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



如何解决这个问题?



任何帮助都会非常感激。



我的尝试:



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:

while (File.Exists(destFile))
 {
   destFile = destFile.Remove(destFile.Length - 4);
                              
   n += 1;
   destFile = string.Format("{0}({1}){2}", destFile, n, ".csv");
  }

推荐答案

是的,因为您使用的是同一个变量( destfile )每次循环你的循环。

迭代1:destfile = file.csv

迭代2:destfile = file(1).csv

迭代3:destfile = file(1)(2).csv



你需要解析任何(n)在添加下一个文件名之前的部分文件名。
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.


您只删除.csv扩展名。因此,如果您的文件名是file(1).csv,它将变为file(1)+(2)+。csv=file(1)(2).csv。



为避免这种情况,您必须将名称拆分为普通名称,紧要计数和扩展名。然后使用递增的计数来构建新名称。



可能的解决方案是使用正则表达式,或者查找开括号和右括号,获取中间的数字并删除那个部分。



另一种解决方案是在循环外部获取简单名称并使用它来建立名称:

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:
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));
}


终于解决了这个问题。



这是我的代码:<使用System.IO获得


; //包括这个命名空间



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。格式({0}({1}){2},destFile,n,。csv);

}
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");
}


这篇关于使用C#自动重命名文件(如果已存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆