自动递增文件名

本文关键字:文件名 | 更新日期: 2023-09-27 18:28:07

我有一个类似的文件列表

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
另一个文件名_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;
    }
}

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

请提出建议。感谢

自动递增文件名

尝试从这里开始:

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包围它以实现名称冲突逻辑。

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

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++;
            }
        }

您可以尝试类似的

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++;
}