Sort files by name c#

Asked

Viewed 205 times

0

I have several files like this:

- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\1_CREATE_TABLE_BMTApprovalGroupExam.sql
- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\10_CREATE_PROC_BMSPInsertNewApprovalRequest2.sql
- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\11_CREATE_PROC_BMSPUpdateApprovalRequest2.sql 
- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\2_CREATE_PROC_BMSPInsertNewApprovalGroupExam.sql

I need to sort the files like this in C#:

- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\1_CREATE_TABLE_BMTApprovalGroupExam.sql
- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\2_CREATE_PROC_BMSPInsertNewApprovalGroupExam.sql
- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\10_CREATE_PROC_BMSPInsertNewApprovalRequest2.sql
- C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\11_CREATE_PROC_BMSPUpdateApprovalRequest2.sql 

I did so:

string[] sqlFiles = Directory.GetFiles(path, "*.sql", SearchOption.TopDirectoryOnly);
foreach (string file in sqlFiles.OrderBy(file => file)) { }

But it’s not working. It’s performing for the first step I’ve taken.

  • Is it certain that the first thing in the file name will be a number? And will always have an underline after?

  • Yes it is right. The first thing in the name will be the number.

  • @Anacarvalho if the number is repeated, the lexicographic order must be the tiebreaker criterion?

  • 1

    if the filename were 01, 02, 10 and 11 would facilitate sorting =]

  • It was, but as it is for my work, it is already defined from the beginning.

  • Good morning Ana. Please could you [Edit] your question and add the information about the full file path? This totally changes the content of the answers that might arise.

  • I corrected the question

Show 2 more comments

2 answers

4


Following @jbueno’s reply, you can use the Path.Getfilename to return only the file name, and proceed with the sorting with the .Split('_')[0], to use only numbers.

See the example below:

var sqlFiles = new[] { @" C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\20170405_TR\1_CREATE_TABLE_BMTApprovalGroupExam.sql",
                       @" C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\2‌0170405_TR\10_CREATE_PROC_BMSPInsertNewApprovalRequest2.sql",
                       @" C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\2‌0170405_TR\11_CREATE_PROC_BMSPUpdateApprovalRequest2.sql",
                       @" C:\ByMe\SOLUTIONS\Dictation1\Database\Updates\2017\2017_04\2‌0170405_TR\2_CREATE_PROC_BMSPInsertNewApprovalGroupExam.sql" };

foreach (string file in sqlFiles.OrderBy(file => Convert.ToInt32(Path.GetFileName(file).Split('_')[0])))

{
    Console.WriteLine(file);
}

See working on . Netfiddle.

  • worked out, thank you very much :)

0

To help those who need help, the answer to the already solved question is as follows::

    string[] sqlFilesLocation = Directory.GetFiles(path, "*.sql", SearchOption.TopDirectoryOnly);
 foreach (string file in sqlFilesLocation.OrderBy(file => Convert.ToInt32(Path.GetFileName(file).Split('_')[0])))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.