C# 自动递增的文件名?
我有一个这样的文件列表 abc.txtC# 自动递增的文件名?,c#,.net,C#,.net,我有一个这样的文件列表 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 .. 文件名_z.txt 另一个文件名\u a001.txt 请注意,计数器再次重置为第一个字符,但附加了一个整数 这是我现在掌握的代码。请注意,它不起作用 st
pas.txt
tempr.txt 我想做的是在这些文件名后面加上英文字母。。 结果应该是这样的 abc_a.txt
pas_b.txt
tempr_c.txt 此过程应持续到最后一个字符(即“z”)。如果有更多文件,则文件名将变为 abc_a.txt
pas_b.txt
tempr_c.txt
..
文件名_z.txt
另一个文件名\u 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;
}
}
string alphabets=“abcdefghijklmnopqrstuvxyz”;
列表文件名=新列表();
添加(“test1.txt”);
添加(“newfile.cs”);
Add(“test2.txt”);
添加(“newfile2.cs”);
string currentFileNmae=string.Empty;
foreach(文件名中的字符串s){
char usedAlphabet=new char();
对于(int i=0;i尝试从这里开始:
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));
}
使用系统诊断;
使用System.IO;
字符串文件名=@“C:\Foo\Bar.txt”;
用于(int count=0;count<100;count++)
{
字符字母=(字符)((int)“a”+计数%26);
字符串数值=(count/26)==0?”:(count/26).ToString(“000”);
Debug.Print(Path.GetFileNameWithoutExtension(文件名)+“”+字母+数字+Path.GetExtension(文件名));
}
替换您自己的循环以遍历文件名,并使用Path
操作名称的各个部分
重命名IIRC可以通过File.Move
来处理。用try
/catch
围绕它来实现名称冲突逻辑。您可以尝试类似的方法
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++;
}
我还没有咖啡,但这应该可以
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++;
}
}
List files=newlist();
int charIndex=0;
int numericIndex=-1;
foreach(files.Select中的var文件(path=>newfileinfo(path)))
{
//创建新文件名-这可能需要一些调整
//要真正只删除扩展名和末尾,请执行以下操作:
//它不能处理像这样的事情
//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.MoveTo(newFileName);
//增量计数器。
如果(charIndex>25)
{
charIndex=0;
数值索引++;
}
}
您尝试了什么?您遇到了什么问题?您是在问如何检查文件是否存在?如何构建计数器?如果您首先告诉我们您这样做的原因,我们可能会提供更好的解决方案。更新的问题…谢谢您是一个问题还是您期待的事情?当您尝试重命名Foo.txt
并发现已经有一个Foo_j042.txt
你会怎么做?在它上面附加一个数字。如果有一个副本,它将被重命名为Foo_j042_1.txt。如果有多个副本,它们将被重命名为Foo_j042_2.txt和Foo_j042_3.txt等。等等。咖啡或不咖啡-但这样做有效ks..很抱歉回复太晚..是在PTO上。。