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));
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));
}
//from your code:
DateTime fileDate;
var newFileName = newFileName(Path.GetFileName(file), out fileDate);
File.Move(newFileName);
string newFileName(string oldFileName, out DateTime fileDate)
{
var fileDate = DateTime.ParseExact(oldFileName.Take(8),
"yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None) ;
var nfn = fileDate.Day.ToString() + "." + fileDay.Month.ToString("MMM") + fileDay.Year.ToString("YYYY") + ".pdf";
return nfn;
}
string path = @"path";
System.IO.DirectoryInfo dir = System.IO.DirectoryInfo(path);
foreach System.IO.FileInfo file in dir.GetFiles()){
string newname = GetNewName(file.Name);
if(newname!=file.Name)
file.Move(file.FullName, System.IO.Path.Combine(file.DiretoryName, newname);
}
public string GetNewName(string f){
string without_ext = f.split('.').First();
string result = f;
Match m = Regex.Match(without_ext, "(\\d{4})(\\d[012]?)(\\d[0123]?\\d)")
if(m.Success){
result = $"{m.Groups[3].Value}.{CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(int.Parse(m.Groups[2].Value)} {m.Groups[1].Value).{f.split('.').Last()}};
}
return result;
}
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.