Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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");
 
<pre>
    arg_proc = "-o " + "\"" + fileWatch.Path
    + "\\" + fout + "%03d_" + cnt + ".tif\" -r " + "\"" + openDialog.FileName + "\"";
 
} while (exist1.Length &gt; 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 12-Jul-13 13:29pm
Josheco127
Edited 12-Jul-13 14:01pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

private static string numberPattern = " ({0})";
 
   public static string NextAvailableFilename(string path)
   {
       // Short-cut if already available
       if (!File.Exists(path))
           return path;
 
<pre>
   // 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[^]
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 325
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web02 | 2.8.150331.1 | Last Updated 12 Jul 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100