SQLLearner
SQLLearner

Reputation: 13

Directory.EnumerateFiles - Get Files which start with certain number and greater than that same number?

I am trying to get list of files which starts with "6" and greater than this number. I have following list of files in a folder. In case of failure of certain file consider 6_q.sql I want to begin with that file and proceed ahead in ascending order.

1_q.sql
2_q.sql
6_q.sql
7_q.sql
8_q.sql

This is my current code, however I used StartsWith and it only takes 6_q.sql file not rest 7_q.sql and 8_q.sql in this case.

var files = Directory.EnumerateFiles(SQLScriptsFolderPath, "*.sql")
           .Where(filename => Path.GetFileName(filename).StartsWith("6"))
           .OrderBy(filename => filename);

May I know what can I use to take files starting with 6 and great then that in acending order?

Edit 1 - Filenames can have any characters after number like 11qq.sql

Upvotes: 1

Views: 1354

Answers (3)

Arsalan Valoojerdi
Arsalan Valoojerdi

Reputation: 1026

Try this:

    static void Main(string[] args)
    {
        var files = Directory.EnumerateFiles(@"yourpath", "*.sql")
            .Where(filename => GetNumberPart(Path.GetFileName(filename)) >= 6)
            .Select(Path.GetFileName);

        foreach (var file in files)
        {
            Console.WriteLine(file);
        }

        Console.ReadKey();
    }

    public static int GetNumberPart(string value)
    {
        return Convert.ToInt32(Regex.Match(value, @"^\d+").Value);
    }

Upvotes: 0

John Wu
John Wu

Reputation: 52250

First, write a small function to convert a file name to a number that can be compared. One way to do it:

int FileNumber(string input)
{
    string numericString = new string(input.Where( c => char.IsDigit(c) ).ToArray());
    bool ok = int.TryParse(numericString, out int result);
    if (ok) return result;
    return default(int);
}

Once you have that it's pretty easy:

var files = Directory.EnumerateFiles(SQLScriptsFolderPath, "*.sql")
           .Where(filename => FileNumber(filename) >= 6 )
           .OrderBy(filename => filename);
    

If you're picky about the file order you may need something slightly more complex:

var files = Directory.EnumerateFiles(SQLScriptsFolderPath, "*.sql")
           .Select( x => new { FileName = x, FileNumber = FileNumber(x) })
           .Where( x => x.FileNumber >= 6 )
           .OrderBy( x => x.FileNumber );
           .Select( x => x.FileName )

Upvotes: 0

Guru Stron
Guru Stron

Reputation: 142158

If file order is not important for you, you can just order files by filename us SkipWhile:

var files = Directory.EnumerateFiles(SQLScriptsFolderPath, "*.sql")
       .OrderBy(filename => filename)
       .SkipWhile(filename => Path.GetFileName(filename) != failedFileName)
       .Skip(1);

If ordering by number is important (and number at start of filename is always present) then you will need to parse that number, convert it to int and order by it:

var reg = new Regex(@"^\d+");
var files = Directory.EnumerateFiles(SQLScriptsFolderPath, "*.sql")
       .Select(file => (n: int.Parse(reg.Match(Path.GetFileName(file)).Value), file))
       .Where(t => t.n > failedFileNumber)
       .OrderBy(t => t.n)
       .Select(t => t.file);

Upvotes: 1

Related Questions