Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
...
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 + "\"";
}
...


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.
Posted
Updated 12-Jul-13 13:01pm
v2

1 solution

C#
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);
   }


See this thread : 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-exists[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900