1

I am trying to write large number of files containing strings. Files have to be named alphabetically (a,b,c..). or anything like 1, 11, 12 13, 2 ,21.. as long as they are alphabetically sorted.

One way I could think of is to use DateTime.Now with milliseconds. Is there any better way to achieve this.

sam
  • 115
  • 7
  • 1
    a, b, c, ..., z, aa, ab ? – Rubens Farias Jan 26 '16 at 01:25
  • 1
    Using only letters you only need 5 characters to give you over 11 million unique file names, so I would start with `aaaaa`, `aaaab`, `aaaac` and so on. – DavidG Jan 26 '16 at 01:31
  • 1
    Why so complicated? **a**, **aa**, **aaa**,..., **aaa....aaaa**,... Do the files have to be named with alphabets only or do they have to be only sorted alphabetically? What kind of homework is that? – Santhos Jan 26 '16 at 01:32
  • You should read [ask]. – Enigmativity Jan 26 '16 at 01:35
  • @RubensFarias a,b,c .. z aa are not alphabetical a, aa, aaa, b ,bc ,c are alphabetical!! It's not duplicate of other question as well ... – sam Jan 26 '16 at 18:08
  • @Santhos file can be named anything as long as they don;t violate windows max file path restriction. Using a,aa ,aaa it would violate at some point – sam Jan 26 '16 at 18:10
  • 1
    @sum Can you refine your filename requirement, with some more precise examples? – Rubens Farias Jan 26 '16 at 18:39

3 Answers3

2

To make it easier, you'll need to make sure all filenames contains the same amount of characters:

DateTime.Now.ToString("yyyyMMdd-HHmmss-fffffff") // "20160126-165224-5464781"


var sequence = 0;
// ...
(++sequence).ToString("0000000000"); // "0000000001"

All files created like this pattern will be in alphabetic order.

The DateTime approach would be my first choice, as it is naturally progressive and I can run my program several times without worrying overwriting anything.

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • thank you for the ++sequence solution. Is there any way we can remove hard-coding of number of zeros required in .ToString("00000000"). I thought of using DateTime approach but file names would not be so intuitive, but I can live with that :) – sam Jan 26 '16 at 19:02
  • You need that number format hardcoded, as the "magic" occurs in the last filename character. If you remove it, you'll have '1, 10, 11, 2' and you don't want that – Rubens Farias Jan 26 '16 at 19:07
  • just wanted to say about last filename character :) – sam Jan 26 '16 at 19:11
  • 1
    Instead of `.ToString("0000000000")` you can use `.ToString("D10")`, for integer types. – Jeppe Stig Nielsen Jan 26 '16 at 20:53
0

use an integer counter and then convert the number to string and replace '0' with 'a', '1', with 'b' etc.

Jens Meinecke
  • 2,904
  • 17
  • 20
  • 2
    why a, b? I guess int.ToString().PadLeft(10, '0') will do the job – Ramazan Binarbasi Jan 26 '16 at 01:27
  • @RamazanBinarbasi - I guess I was taken by the `named alphabetically (a, b, c...)` bit in the question. But I guess you are right - on second reading it appears that the names can be sorted by means of a string sort in the correct order. In that case `counter.ToString("000000");` would do the trick. – Jens Meinecke Jan 26 '16 at 01:35
  • @Übercoder for end files I believe it give something similar 100001, 10002... 110000 but they are not alphabetical ordered its numerically ordered. Right sequence would be 100001,110000, 10002. Please read my edit on questions and correct me if I am wrong. – sam Jan 26 '16 at 18:32
0

Here's how you can do the alpha sort ('a-z', 'aa-zz', etc):

public static void GenerateFileNames(int totalFiles)
{
    for (int i = 0; i < totalFiles; i++)
    {
        Console.WriteLine(GetString(i));
    }
}

public static string GetString(int index)
{
    if (index < 26)
    {
        return ((char)('A' + index)).ToString();
    }

    return GetString(index / 26 - 1) + GetString(index % 26);
}

Simply call GenerateFileNames() with the total number of files and the GetString() function will return the file name starting with A and going up alphabetically through the last filename.

This answer was taken from the linked question and works perfectly to do what you're asking. The accepted answer also contains another method to accomplish the same.

If you want to use more letters in your filenames (ex. 'AAAA'), here's a method you can use to get the index:

public static int GetStartIndex(string letters)
{
    int index = 0;

    for (int i = letters.Length - 1; i > 0; i--)
    {
        index += (int)Math.Pow(26, i);
    }

    return index;
}

And with an overload on GenerateFileNames():

public static void GenerateFileNames(int totalFiles, int startIndex)
{
    for (int i = startIndex; i < totalFiles + startIndex; i++)
    {
        Console.WriteLine(GetString(i));
    }
}

You can call GenerateFileNames(100, GetStartIndex("AAAA")); to write 100 files alpha sorted, starting with AAAA.

EDIT:

I should note that for strings much longer than AAAA, you may want to use long instead of int, because exponents. The numbers grow quickly! This method should work as is for files up to 7 letters in length, and a good bit higher with conversion to long.

EDIT 2:

To sort these files alphabetically, you can simply use the .Sort() method on a List as such:

public static List<string> GenerateSortedFileNames(int totalFiles)
{
    List<string> names = new List<string>();

    for (int i = 0; i < totalFiles; i++)
    {
        names.Add(GetString(i));
    }

    names.Sort();


    return names;
}

Then use it like this:

List<string> SortedNames = GenerateSortedFileNames(100);

foreach (string fileName in SortedNames)
{
    Console.WriteLine(fileName);
}
//Outputs A,AA,AB,AC ... X,Y,Z
Community
  • 1
  • 1
levelonehuman
  • 1,465
  • 14
  • 23
  • @levelonehumar, I tried running GenerateFileNames(1000), output is a,b,c....z,aa,ab. Is there any way to get all results sorted instead a-z sorted then aa-zz sorted kinda output. – sam Jan 26 '16 at 21:48
  • @sum I have updated the answer to show alphabetizing the file names. If you use the other method that takes a starting set (such as `AAAA`), then this might be a moot point, as `AAAA` - `ZZZZ` allows for many thousands of combinations that will already be generated alphabetically. – levelonehuman Jan 26 '16 at 22:03
  • Answer is correct but it doesn't solve problem. Sorting list of strings after creating it is not the problem, list of strings created should be sorted. As I have mentioned in the problem I have to write large number of files(number is unknown), I cannot keep already populated list of string. Though this solution can be used but it would be misuse of technology :) – sam Jan 27 '16 at 01:08