Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI
I want to sort files in a directory containing .sql files named as

1.sql
2.sql
3.sql
....
....
....
n.sql

I want to sort filed by number

I have written this code

C#
string sqlConnectionString = cn.ConnectionString.ToString();
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            string script = string.Empty;
            Server server = new Server(new ServerConnection(conn));
            MessageBox.Show(server.Information.Version.ToString());
            bool IsFailed = false;
            string mappath = Path.Combine(Application.StartupPath.ToString(), "Scripts");
            //var files1 = from file in Directory.GetFiles(mappath)
            //            orderby file 
            //            select file;
            //var biggest = files1.First();
            //var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(f => new FileInfo(f).Name);
            var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => n);
            string[] filePaths = Directory.GetFiles(@"" + mappath + "", "*.sql");



Would you please resolve this error
Posted
Updated 30-Jul-15 20:00pm
v4
Comments
DamithSL 31-Jul-15 1:44am    
what is the issue with
Directory.GetFiles(mappath, "*.sql").OrderBy(f => f) ?
TarunKumarSusarapu 31-Jul-15 1:53am    
It is not getting with appropriate result after 1.sql it is showing 10.sql

The problem is in your OrderBy(n => n), is does an alpha sort.

You need to convert the n to the num value.
Try to sort on something like Convert.ToInt32(n) rather than on n
 
Share this answer
 
If your file names contains only numeric values then try this-
C#
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => Convert.ToInt16(Path.GetFileNameWithoutExtension(n)));


Else you can write a function based on the logic shared here
http://stackoverflow.com/a/5093939/1006297[^]
C#
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(n => PadNumericPortion(n));


Hope, it helps :)
 
Share this answer
 
v3
I have written this code Now it is getting Correctly

C#
var files = Directory.GetFiles(@"" + mappath + "", "*.sql").OrderBy(f=>int.Parse(Path.GetFileNameWithoutExtension(f)));
 
Share this answer
 
Comments
Suvendu Shekhar Giri 31-Jul-15 2:19am    
Perfect !
Good to see that you have not stopped trying after posting the question here.

5ed for that :)
TarunKumarSusarapu 31-Jul-15 2:29am    
Thank a lot
Matt T Heffron 31-Jul-15 13:04pm    
Just a side question:
Why do you do
@"" + mappath + ""
This accomplishes nothing!
mappath is already a string and concatenating an empty string at each end does nothing.
(Also, Application.StartupPath is already a string, so there's no need to use .ToString())

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900