Search code examples
c#filepdffile-rename

change file name c# with a loop


I have to Change the Name of some pdfs(1,2 TB). The Name of the files is for example 20160112_0(year/month/day/_0). I have to Change the file Name to tu, 12.January 2016(weekday/day/month/year).

I allready got the filename but i dont know how to Change it right.

string dir = @"path";
        string[] files = Directory.GetFiles(dir);
        foreach (string file in files)
            Console.WriteLine(Path.GetFileName(file));

Solution

  • Try this

            string dir = @"YourPath";
            string fileDate, new_fileDate;
            DateTime dt;
            foreach (string original_filename in Directory.GetFiles(dir))
            {
                fileDate = Path.GetFileName(original_filename).Substring(0, 8);
                dt = DateTime.ParseExact(fileDate, "yyyyMMdd", CultureInfo.InvariantCulture);
                new_fileDate = dt.ToString("ddd_dd_MMMM_yyyy");
                File.Move(original_filename, original_filename.Replace(fileDate, new_fileDate));
            }