简体   繁体   中英

Renaming mulitple files from bottom to top? C#

i have put together this code which renames all the files in a folder in numeric order. What i want to do is make the last image have the name "1", 2nd to last image be named "2" if you catch my drift. im not sure how to do it. i have this so far

try
{
    string Path = @"C:\Users\William\Pictures\Documents\Apple iPhone\";
    DirectoryInfo di = new DirectoryInfo(Path);
    FileInfo[] fiArr = di.GetFiles("*.jpg");
    int i = 1;

    string path;

    foreach (FileInfo fri in fiArr)
    {
        path = Path + i.ToString() + ".jpg";

        fri.MoveTo(path);

        i++;

    }
}
catch { }

Try this:

try
{
    string Path = @"C:\Users\William\Pictures\Documents\Apple iPhone\";
    DirectoryInfo di = new DirectoryInfo(Path);
    FileInfo[] fiArr = di.GetFiles("*.jpg");

    for (var i = fiArr.Length; i > 0; i--)
    {
        var fri = fiArr[i - 1];
        var path = Path + i.ToString() + ".jpg";

        fri.MoveTo(path);
    }
}
catch { }

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