/// <summary> /// It checks exist files in folders /// </summary> /// <param name="names">The list of files names</param> /// <param name="oldDir">The directory where ley these files</param> /// <param name="newDir">The directory where you copy or move these files</param> /// <returns>Errors</returns> private string CheckExistFilesAndDirectories(List <string> names, string oldDir, string newDir = null) { string errorMessage = String.Empty; DirectoriesList newFilesError = new DirectoriesList(); DirectoriesList oldFilesError = new DirectoriesList(); newFilesError.names = new List <string>(); oldFilesError.names = new List <string>(); if (!String.IsNullOrEmpty(oldDir)) { oldFilesError.dir = new DirectoryInfo(oldDir).Name; if (!Directory.Exists(oldDir)) { return("The directory " + oldFilesError.dir + " don't exist!"); } } if (!String.IsNullOrEmpty(newDir)) { newFilesError.dir = new DirectoryInfo(newDir).Name; if (!Directory.Exists(newDir)) { return("The directory " + newFilesError.dir + " don't exist!"); } } foreach (var file in names) { if (!String.IsNullOrEmpty(oldDir)) { string pathOld = Path.Combine(oldDir, file); if (!System.IO.File.Exists(pathOld)) { oldFilesError.names.Add(file); } } if (!String.IsNullOrEmpty(newDir)) { string pathNew = Path.Combine(newDir, file); if (System.IO.File.Exists(pathNew)) { newFilesError.names.Add(file); } } } if (!String.IsNullOrEmpty(oldDir)) { errorMessage += WriteErrors(oldFilesError, errorMessage, "The follow files don't exist in the folder "); } if (!String.IsNullOrEmpty(newDir)) { errorMessage += WriteErrors(newFilesError, errorMessage, "The follow files already exist in the folder "); } return(errorMessage); }
private string WriteErrors(DirectoriesList filesError, string errorMessage, string textMessage) { if ((filesError.names != null) && (filesError.names.Count > 0)) { errorMessage = textMessage + filesError.dir + ": "; for (var i = 0; i < filesError.names.Count; i++) { errorMessage += filesError.names[0]; if ((i + 1) < filesError.names.Count) { errorMessage += ", "; } else { errorMessage += ". "; } } } return(errorMessage); }
public IActionResult RemoteFiles([FromBody] DirectoriesList value) { if (value == null) { return(Ok(new { error = "Error in the request! Try again..." })); } // It checks exist of files in folders string errorMessage = CheckExistFilesAndDirectories(value.names, value.dir); if (!String.IsNullOrEmpty(errorMessage)) { return(Ok(new { error = errorMessage })); } // If it's Ok, it copies files in the folder foreach (var file in value.names) { string path = Path.Combine(value.dir, file); System.IO.File.Delete(path); } return(Ok(new { error = "" })); }