将编号附加到要保存的文件中. [英] Append number to a file being saved.
问题描述
大家好
我想保存文件,并在文件名的末尾附加数字.例如,如果已经将名为test的文件保存在文件夹中,则下一次用户下次尝试保存另一个名为test的文件时,应将其另存为test( 1).n第三次作为test(2)n.依此类推.目前,我正在使用以下函数,但我想要一个更好的函数:
Hi all
Im want to save file and append the number at the end of the file name.Example if a file named test is alread saved in a folder then the next time a user tries to save another file named test then it should be saved as test(1).n the 3rd time as test(2) n so on.Currently im using the function below but i want a more better one:
private string GetUniqueName(string name, string folderPath)
{
string validatedName = name;
int tries = 1;
while (File.Exists(folderPath + validatedName))
{
validatedName = string.Format("{0} [{1}]", name, tries++);
}
return validatedName;
}
此功能在文件名(2)处停止,如果需要保存具有相同名称的第三个文件,则无法执行此操作.我们无法确定用户在一段时间内可能要保存的具有相同名称的文件数.这就是为什么我正在寻找更好的方法的原因
谢谢,
This function stops at filename(2)and if a 3rd file with the same name needs to be save it can not do so.We cant we sure of the number of files with the same name a user may want to save over a period of time that is why im looking for a better approach
Thanks
推荐答案
我为您编写了一个简单的解决方案..每次您检查文件名时,都需要检查最新的数字,这就是为什么您的代码没有检查超过1.
Hi, I have coded a simple solution for you.. every time you check the file name you need to check for the latest number that''s why your code is not checking beyond 1.
private string GetUniqueName(string name, string folderPath, string extension)
{
string validatedName = name;
//first get all the files with the name and the number
string [] dirs = Directory.GetFiles(folderPath, name + "*" + extension);
if (dirs.Count() > 0)
{
Array.Sort(dirs);
//get the lastfilename from the array
string CurrentLastFile = dirs[dirs.Length -1];
//use a regex to get the number from the lastfilename
Match m = Regex.Match(CurrentLastFile, "\\d+");
int number = m.Value == "" ? 0 : Convert.ToInt32(m.Value);
//assign the next number in the sequence
validatedName = string.Format("{0} [{1}]", name, number + 1);
}
return validatedName;
}
首先,我从目录中获取所有具有相同名称格式的可用文件,然后从列表中获取最后一个文件名,并获取与之关联的编号.新文件名将根据最后一个文件名形成..希望您能想到..
First I am getting all the available files in the same name format from the directory then I am getting the last file name from the list and get the number associated with it. The new file name will be formed based on the last file name.. Hope you get the idea..
感谢大家的辛苦.下面的代码对我来说很有效.希望这可以帮助别人.
Thanks everyone for your effort.The code below work well for me.Hope this can help someone else.
private static string numberPattern = " ({0})";
public static string NextAvailableFilename(string path)
{
// Short-cut if already available
if (!File.Exists(path))
return path;
// If path has extension then insert the number pattern just before the extension and return next filename
if (Path.HasExtension(path))
return GetNextFilename(path.Insert(path.LastIndexOf(Path.GetExtension(path)), numberPattern));
// Otherwise just append the pattern to the path and return next filename
return GetNextFilename(path + numberPattern);
}
private static string GetNextFilename(string pattern)
{
string tmp = string.Format(pattern, 1);
if (tmp == pattern)
throw new ArgumentException("The pattern must include an index place-holder", "pattern");
if (!File.Exists(tmp))
return tmp; // short-circuit if no matches
int min = 1, max = 2; // min is inclusive, max is exclusive/untested
while (File.Exists(string.Format(pattern, max)))
{
min = max;
max *= 2;
}
while (max != min + 1)
{
int pivot = (max + min) / 2;
if (File.Exists(string.Format(pattern, pivot)))
min = pivot;
else
max = pivot;
}
return string.Format(pattern, max);
}
这篇关于将编号附加到要保存的文件中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!