如果存在上一组文件,则将自动增量添加到文件名末尾 [英] Add autoincrement to end of filename if previous set of files exist
问题描述
我需要创建x个文件(一组),但我必须首先检查文件是否存在类似名称。
例如,tiftest1.tif,tiftest2.tif,...存在,我必须再次将tiftest写入同一目录。我想将_x附加到文件名的末尾,其中x是一个自动递增的数字,每次我想要创建该集合。所以我可以有tiftest1_1.tif,tiftest2_1.tif,tiftest1_2.tif,tiftest2_2.tif,tiftest1_3.tif,tiftest2_3.tif等等。
这是什么我到目前为止:
I need to create x number of files (a set) but I must first check to see if the files exist with a similar name.
For example, tiftest1.tif, tiftest2.tif, ... exist and I must write tiftest again to the same directory. I'd like to append _x to the end of the filename, where x is a number that is auto-incremented, each time that I want to create the set. So I can have tiftest1_1.tif,tiftest2_1.tif, tiftest1_2.tif, tiftest2_2.tif, tiftest1_3.tif, tiftest2_3.tif and so forth.
Here is what I have so far:
...
DirectoryInfo root = new DirectoryInfo(fileWatch.Path);
FileInfo[] exist = root.GetFiles(fout + "*.tif");
if (exist.Length > 0)
{
int cnt = 0;
do
{
cnt++;
DirectoryInfo root1 = new DirectoryInfo(fileWatch.Path);
FileInfo[] exist1 = root.GetFiles(fout + "*" + "_" + cnt + ".tif");
arg_proc = "-o " + "\"" + fileWatch.Path
+ "\\" + fout + "%03d_" + cnt + ".tif\" -r " + "\"" + openDialog.FileName + "\"";
} while (exist1.Length > 0); //exist1 is out of scope so this doesn't work
}
else
{
arg_proc = "-o " + "\"" + fileWatch.Path
+ "\\" + fout + "%03d.tif\" -r " + "\"" + openDialog.FileName + "\"";
}
...
exists1.length超出范围,因此循环将持续运行。我不确定如何纠正这个问题。我的方法是最初扫描目录以查找匹配项,看看数组的长度是否大于0.如果是> 0然后_x将自动递增,直到找不到匹配项。 arg_proc是函数(未包含)中使用的字符串,它将创建文件。
exist1.length is out of scope so the loop will continually run. I'm not certain how to correct this. My method was to originally scan the directory for a match and see if the length of the array is greater than 0. If it is > 0 then the _x will autoincrement until a match isn't found. arg_proc is a string used in a function (not included) which will create the files.
推荐答案
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);
}
见这个帖子: http://stackoverflow.com/questions/1078003/c-how-would-you-make-a-unique- filename-by-adding-a-number [ ^ ]
http://stackoverflow.com//questions/6264098/how-to-increment-the-filename-if -file-already-exist [ ^ ]
这篇关于如果存在上一组文件,则将自动增量添加到文件名末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!