0

below xml is for image output purpose, there is no any issue on output image using xml code below for me.

public static void TIF(Document dc, SortedList<string, object> dcIndexes)
{
    string str1 = dcIndexes["Document,name"].ToString();
    string str2 = dcIndexes["Document,age"].ToString();
    string str5 = dcIndexes["Document,status"].ToString();
  string str3 = Path.Combine(Config.OutputFolder, DateTime.Now.ToString("yyyyMMdd"));
  if (!Directory.Exists(str3))
    Directory.CreateDirectory(str3);
  string str4 = Path.Combine(str3, str1 + "_" + str2 + "_" + str5 + ".tif");
  DocumentHistory dh = (DocumentHistory) null;
  string sourceFileName = ServiceES.FromSE(dc, out dh);
  if (File.Exists(str4))
    File.Delete(str4);
  File.Move(sourceFileName, str4);
  PTrace.LogInformation("{0} - TIF - {1}", (object) dc.Title, (object) str4);
}

the output path now is \YYYYMMDD\NAME_AGE_STATUS.tif if "name" is empty, image name will become: \YYYYMMDD\_AGE_STATUS.tif the issue i having now is, there are same age and status within the database. the image will just replace over write the first image follow by second and third image, end up only 1 image.

may i know how can i add a running number behind ? example: \YYYYMMDD\_AGE_STATUS.tif (first image), \YYYYMMDD\_AGE_STATUS_001.tif (second image) follow by \YYYYMMDD\_AGE_STATUS_002.tif (third image). if can remove the "_" front of AGE will look better. example:\YYYYMMDD\AGE_STATUS_002.tif` its seem because of code below not allow:

 if (File.Exists(str4))
  File.Delete(str4);

how can i add a sequence number 001,002,003 behind if its duplicate tif ? 10 of file name now duplicated.

_23_Single
_23_Single
_23_Single
_23_Single
_23_Single
_23_Single
_23_Single
_23_Single
_23_Single
_23_Single

to become:

    _23_Single
    _23_Single_001
    _23_Single_002
    _23_Single_003
    _23_Single_004
    _23_Single_005
    _23_Single_006
    _23_Single_007
    _23_Single_008
    _23_Single_009

or to become:

23_Single
23_Single_001
23_Single_002
23_Single_003
23_Single_004
23_Single_005
23_Single_006
23_Single_007
23_Single_008
23_Single_009
user2523188
  • 61
  • 11
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackoverflow.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 05 '14 at 15:21
  • 1
    Please don't call your variables `str1` through `str15`. Proper variable naming makes understanding the code much easier. `str1` could be called `docName` for example. – Tomalak May 05 '14 at 15:38
  • Possible duplicate of [C#: How would you make a unique filename by adding a number?](http://stackoverflow.com/questions/1078003/c-how-would-you-make-a-unique-filename-by-adding-a-number) – Matthew Strawbridge May 04 '17 at 16:54

2 Answers2

2

You'll have to create the file name, check it, and then change it if the file exists. For example:

public static void TIF(Document dc, SortedList<string, object> dcIndexes)
{
    string str1 = dcIndexes["Document,name"].ToString();
    string str2 = dcIndexes["Document,age"].ToString();
    string str5 = dcIndexes["Document,status"].ToString();
    string str3 = Path.Combine(Config.OutputFolder, DateTime.Now.ToString("yyyyMMdd"));
    if (!Directory.Exists(str3))
        Directory.CreateDirectory(str3);

    // ********
    // find first available file name
    bool done = false;
    int sequence = 0;
    string str4;
    string baseName = str1 + "_" + str2 + "_" + str5;
    do
    {
        // change here ***
        string fname = baseName;
        if (sequence > 0)
            fname = fname + "_" + sequence.ToString();
        // end of change ***

        str4 = Path.Combine(str3, fname + ".tif");
        if (File.Exists(str4))
            ++sequence;
        else
            done = true;
    } while (!done);

    // str4 now contains the file name
    // ********

    DocumentHistory dh = (DocumentHistory) null;
    string sourceFileName = ServiceES.FromSE(dc, out dh);
    if (File.Exists(str4))
        File.Delete(str4);
    File.Move(sourceFileName, str4);
    PTrace.LogInformation("{0} - TIF - {1}", (object) dc.Title, (object) str4);
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • hi, can u direct edit from my code and i can replace it back to my place. because i have no idea where should i paste your code into which line. – user2523188 May 05 '14 at 15:38
  • @user2523188: I've updated my answer to show where you would insert that code. Note also that your code would be much easier to understand if you used meaningful variable names rather than `str1`, `str3`, etc. – Jim Mischel May 05 '14 at 21:02
  • its works perfect!! but if the file not exists, all the file name also named as _0 ? e.g.: name_age_status_0 – user2523188 May 06 '14 at 02:52
  • @user2523188: I modified my example to show how you'd change it to generate the list of names the way you wanted them. – Jim Mischel May 06 '14 at 03:15
0

I would just count, how many files already exist

string fileName = // your file name
int filesWithThisName = 1;
while(File.Exists(fileName))
{
  fileName = Path.Combine(str3, str1 + "_" + str2 + "_" + str5 + "_" + i.ToString("D3") + ".tif");
  i++;
}
// save file
Chrisi
  • 371
  • 3
  • 15
  • You could do that by calling `Directory.GetFiles`, although that's going to give you all of the files that exist, even if there's a gap. That is, if `x_001` and `x_003` exist, but not `x_002`, your code will stop at 2 and `Directory.GetFiles` will give you all of the files. – Jim Mischel May 05 '14 at 15:34