How to retrieve a numbered sequence range from a List of filenames?

Posted by glenneroo on Stack Overflow See other posts from Stack Overflow or by glenneroo
Published on 2010-12-24T00:06:09Z Indexed on 2010/12/24 0:54 UTC
Read the original article Hit count: 264

Filed under:
|
|
|

I would like to automatically parse the entire numbered sequence range of a List<FileData> of filenames (sans extensions) by checking which part of the filename changes.

Here is an example (file extension already removed):

First filename: IMG_0000
Last filename: IMG_1000
Numbered Range I need: 0000 to 1000

Except I need to deal with every possible type of file naming convention such as:

0000 ... 9999
20080312_0000 ... 20080312_9999
IMG_0000 - Copy ... IMG_9999 - Copy
8er_green3_00001 .. 8er_green3_09999
etc.

  • I need the entire 0-padded range e.g. 0001 not just 1
  • The sequence number is 0-padded e.g. 0001
  • The sequence number can be located anywhere e.g. IMG_0000 - Copy
  • The range can start and end with anything i.e. doesn't have to start with 1 and end with 9999

Whenever I get something working for 8 random test cases, the 9th test breaks everything and I end up re-starting from scratch.

I've currently been comparing only the first and last filenames (as opposed to iterating through all filenames):

void FindRange(List<FileData> files, out string startRange, out string endRange)
{
    string firstFile = files.First().ShortName;
    string lastFile = files.Last().ShortName;

    ...
}

Does anyone have any clever ideas?

© Stack Overflow or respective owner

Related posts about c#

Related posts about parsing