0

我有一个像这样的文件列表

abc.txt
pas.txt
tempr.txt

我想做的是将英文字母附加到这些文件名..结果应该是这样的

abc_a.txt
pas_b.txt
tempr_c.txt

这个过程应该一直持续到最后一个字符(即'z')。如果有更多文件,则文件名将变为

abc_a.txt
pas_b.txt
tempr_c.txt
.................
filename_z.txt
anotherfilename_a001.txt

请注意,计数器再次重置为第一个字符,但附加了一个整数。

这是我现在拥有的代码。请注意,它不起作用..

string alphabets= "abcdefghijklmnopqrstuvwxyz";
List<string> filenames = new List<string>();
filenames.Add("test1.txt");
filenames.Add("newfile.cs");
filenames.Add("test2.txt");
filenames.Add("newfile2.cs");


string currentFileNmae = string.Empty;

foreach(string s in filenames) {
    char usedAlphabet = new char();
    for(int i = 0;i<=alphabets.Length-1;i+=11) {
        usedAlphabet.Dump();
        alphabets[i].Dump();
        if(usedAlphabet != alphabets[i] )
        {
            if(currentFileNmae!= s)  
            {
                string.Format("{0}--{1}",s,alphabets[i]).Dump();
                usedAlphabet = alphabets[i];
                currentFileNmae = s;
            }

        }


        break;

    }

}

我是一个团队的一员,该团队正在为我们的内部目的构建文件重命名器工具,因此我需要这段代码。这是我们计划的枚举功能的一部分。

请建议。谢谢

4

3 回答 3

1

尝试从这里开始:

using System.Diagnostics;
using System.IO;

string filename = @"C:\Foo\Bar.txt";

for (int count = 0; count < 100; count++)
{
    char letter = (char)((int)'a' + count % 26);
    string numeric = (count / 26) == 0 ? "" : (count / 26).ToString("000");
    Debug.Print(Path.GetFileNameWithoutExtension(filename) + "_" + letter + numeric + Path.GetExtension(filename));
}

替换您自己的循环来遍历文件名并用于Path操作名称的片段/部分。

重命名 IIRC 可以由File.Move. try用/包围它catch以实现名称冲突逻辑。

于 2013-08-18T03:20:40.280 回答
0

还没有咖啡,但这应该可以。

List<string> files = new List<string>();


        int charIndex = 0;
        int numericIndex = -1;

        foreach (var file in files.Select(path => new FileInfo(path)))
        {
            // Create new Filename - This may needs some tuning 
            // to really remove only the extension ad the end
            // It doesnt take care of things like
            // file.bmp.bmp.bmp ...
            string newFileName =  String.Format("{0}_{1}{2}.{3}", 
                file.FullName.Replace(file.Extension,String.Empty),
                (char)(charIndex++ + 97), 
                (numericIndex > -1 ? String.Format("{0:D4}", numericIndex) : String.Empty), 
                file.Extension);

            // Rename the File
            file.MoveTo(newFileName);

            // Increment Counters.
            if (charIndex > 25)
            {
                charIndex = 0;
                numericIndex++;
            }
        }
于 2013-08-18T05:16:16.457 回答
0

你可以试试这样的

const string directory = @"C:\\wherever";
string[] fiNames = new string[]{ "abc", "pas", "etc",};
char[] alphabet =       "abcdefghijklmnopqrstuvwxyz".ToCharArray();
int x = 0;
 string ending = "";
for(int i = fiNames.Count()-1; i>=0; i--)
{
  if(x%26==0)
   {
      x=0 
       if( ending=="")
          ending="1";
       else
          ending=(System.Convert.ToInt32(ending)+1).ToString();
    }
  System.IO.File.Move(directory+fiNames[i], fiNames[i]+alphabet[x].ToString()+ending); 
x++;
}
于 2013-08-18T04:56:38.563 回答