Reputation: 135
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.
Upvotes: 1
Views: 763
Reputation: 1505
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
Upvotes: 0
Reputation: 57996
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.
Upvotes: 2
Reputation: 2940
use an integer counter and then convert the number to string and replace '0' with 'a', '1', with 'b' etc.
Upvotes: 0