简体   繁体   中英

Sort files in ascending order

I have project that converts a pdf to tif image files. And the out put files are numbered in the form. file1, file2, file3.......file20. When I do the code below to get the files, they are arranged in the list as shown below which is not correct. Any ideas how to go around this?

FileInfo[] finfos = di.GetFiles("*.*");

finfos[0]=file1

finfos[1]=file10

finfos[2]=file11

finfos[3]=file12

....
...................

finfos[4]=file19

finfos[5]=file2

finfos[6]=file20

finfos[7]=file3

finfos[7]=file4

If all of the files are named mypic<number>.tif and there are no files in directory that have different name format, try with this:

        FileInfo[] orderedFI = finfos
            .OrderBy(fi => 
                // This will convert string representation of a number into actual integer
                int.Parse(
                    // this will extract the number from the filename
                    Regex.Match(Path.GetFileNameWithoutExtension(fi.Name), @"(\d+)").Groups[1].Value
                    ))
            .ToArray();

if they are created in order the do sort by date of created.

here is a solution to your problem by using List

class Program
{
    private static int CompareWithNumbers(FileInfo x, FileInfo y)
    {
        if (x == null)
        {
            if (y == null)
            {
                // If x is null and y is null, they're 
                // equal.  
                return 0;
            }
            else
            {
                // If x is null and y is not null, y 
                // is greater.  
                return -1;
            }
        }
        else
        {
            // If x is not null... 
            // 
            if (y == null)
            // ...and y is null, x is greater.
            {
                return 1;
            }
            else
            {

                int retval = x.CreationTime<y.CreationTime?-1:1;
                return retval;          

            }
        }
    }
    static void Main(string[] args)
    {
        DirectoryInfo di = new DirectoryInfo("d:\\temp");
        List<FileInfo> finfos = new List<FileInfo>();
        finfos.AddRange(di.GetFiles("*"));
        finfos.Sort(CompareWithNumbers);

        //you can do what ever you want
    }
}

Leading zeros may be a solution for you. It's not clear from your description if you control the code that generates the files. If not you can use a method to match file1,... file9 (ie regex or filename length) and rename them. If you control the code then use a formatter to convert the number to string with leading zeros (ie for 2 digit numbers {0:00}).

EDIT:

To get a direction play with the following draft sample:

Assume that you have on the execution directory the files: file1.txt, file2.txt, file10.txt, and file20.txt

foreach (string fn in System.IO.Directory.GetFiles(".", "file*.*"))
  if (System.Text.RegularExpressions.Regex.IsMatch(fn, @"file\d.txt"))
    System.IO.File.Move(fn, fn.Replace("file", "file0"));

The above piece of code will rename file1.txt to file01.txt and file2.txt to file02.txt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM