public static void CallRemoveTask(int days, string spath, string cpath) { DateTime morethanNDays = DateTime.Now.AddDays(days); var logpath = "./appLogRemove_" + DateTime.Today.ToString("yyyy.MM.dd") + ".txt"; var failedlogpath = "./appLogFailedRemove_" + DateTime.Today.ToString("yyyy.MM.dd") + ".txt"; if (cpath == spath) { try { Functionlog = !File.Exists(logpath) ? new StreamWriter(logpath) : File.AppendText(logpath); FailedFunctionlog = !File.Exists(failedlogpath) ? new StreamWriter(failedlogpath) : File.AppendText(failedlogpath); } catch (Exception e) { Console.WriteLine(DateTime.Now + " : Cannot open {0} or {1} for writing", logpath, failedlogpath); Console.WriteLine(e.Message); return; } } try { foreach (string nextPath in Directory.GetDirectories(cpath)) { foreach (var file in Directory.GetFiles(nextPath, "*.*")) { AllFileCounter++; FileInfo fi = new FileInfo(file); string fpath = ""; try { if (fi.LastWriteTime < morethanNDays) { fpath = nextPath + "\\" + fi.Name; //Delete File fi.Delete(); var logtext = string.Format(DateTime.Now + " : Remove file : {0} ", fpath); Console.WriteLine(logtext); Functionlog.WriteLineAsync(logtext); FunctionFileCounter++; } } catch (Exception ex) { var logtext = string.Format(DateTime.Now + " : Failed Remove file : {0} Reason is : {1}", fpath, ex); Console.WriteLine(logtext); FailedFunctionlog.WriteLine(logtext); } } CallRemoveTask(days, spath, nextPath); } } catch (Exception excpt) { var logtext = string.Format(DateTime.Now + " : Error = " + excpt.Message); Console.WriteLine(logtext); FailedFunctionlog.WriteLine(logtext); } }
public static void CallMoveTask(int days, string spath, string dpath, string cpath, string excludePaths) { DateTime morethanNDays = DateTime.Now.AddDays(days); var logpath = "./appLogMove_" + DateTime.Today.ToString("yyyy.MM.dd") + ".txt"; var failedlogpath = "./appLogFailedMove_" + DateTime.Today.ToString("yyyy.MM.dd") + ".txt"; if (cpath == spath) { try { Functionlog = !File.Exists(logpath) ? new StreamWriter(logpath) : File.AppendText(logpath); FailedFunctionlog = !File.Exists(failedlogpath) ? new StreamWriter(failedlogpath) : File.AppendText(failedlogpath); } catch (Exception e) { Console.WriteLine(DateTime.Now + " : Cannot open {0} or {1} for writing", logpath, failedlogpath); Console.WriteLine(e.Message); return; } } try { //var excludeflag = false; //var exculdefound = false; string[] excludePathArrays = { }; if (excludePaths != "") { excludePathArrays = excludePaths.Split(','); //excludeflag = true; } foreach (string nextPath in Directory.GetDirectories(cpath).Where(d => !excludePathArrays.Any(x => d.StartsWith(x, StringComparison.OrdinalIgnoreCase)))) { // if (excludeflag) // { // foreach (var expath in excludePathArrays) // { // if (nextPath == expath) // { // exculdefound = true; // break; // } // } // if (exculdefound) // { // var logtext = string.Format(DateTime.Now + " : Ignored Directory From this path : {0} ", nextPath); // Console.WriteLine(logtext); // Functionlog.WriteLineAsync(logtext); // continue; // } //} foreach (var file in Directory.GetFiles(nextPath, "*.*")) { AllFileCounter++; FileInfo fi = new FileInfo(file); var despath = ""; try { //var logfiletext = string.Format(DateTime.Now + " : Path file : {0} Date : {1} < Date {2}", nextPath + "\\" + fi.Name, fi.LastWriteTime, morethanNDays); //Console.WriteLine(logfiletext); //FileInfo fi = new FileInfo(file); if (fi.LastWriteTime < morethanNDays) { despath = dpath + nextPath.Substring(spath.Length) + "\\" + fi.Name; var dirPath = despath.Substring(0, despath.LastIndexOf("\\", StringComparison.Ordinal) + 1); Directory.CreateDirectory(dirPath); // Ensure that the target does not exist. if (File.Exists(despath)) { File.Delete(despath); } // Move the file. File.Move(nextPath + "\\" + fi.Name, despath); var logtext = string.Format(DateTime.Now + " : Move file From : {0} To : {1}", nextPath + "\\" + fi.Name, despath); Console.WriteLine(logtext); Functionlog.WriteLineAsync(logtext); FunctionFileCounter++; } } catch (Exception ex) { var logtext = string.Format(DateTime.Now + " : Failed Move file From : {0} To : {1} Reason is : {2}", nextPath + "\\" + fi.Name, despath, ex); Console.WriteLine(logtext); FailedFunctionlog.WriteLine(logtext); } } CallMoveTask(days, spath, dpath, nextPath, excludePaths); } } catch (Exception excpt) { var logtext = string.Format(DateTime.Now + " : Error = " + excpt.Message); Console.WriteLine(logtext); FailedFunctionlog.WriteLine(logtext); } }