/// <summary> /// <b>Description:</b> /// This will iterate the file system on the given path. /// Prepare the list of files which has passed fileAgeToValidate limit and to be /// deleted. /// Prepare the list of files which yet to pass fileAgeToValidate limit and to be /// stored. /// Check File is not open. /// Delete the file. /// if file has passed fileAgeToValidate and still cannot be deleted, mark file as /// failed to delete and record to database. /// Return the list. /// </summary> /// <param name="fileAgeLimit"></param> /// <param name="cOfFiles"></param> public ScannedFiles ProcessScannedFiles(int fileAgeLimit, FileDateProperty enumFileProperty, ScannedFiles cOfFiles) { processedFile.DatabaseType = DatabaseType; processedFile.SaveToCSVFile = SaveToCSVFile; DateTime fileAuditDate = DateTime.Today; foreach (ScannedFile currentFile in cOfFiles) { string fullFilePath = currentFile.Filepath + currentFile.Filename; if (IsFileExists(fullFilePath)) { if (enumFileProperty == FileDateProperty.DateAdded) { fileAuditDate = currentFile.LastAccessTime; } else if (enumFileProperty == FileDateProperty.DateModified) { fileAuditDate = currentFile.LastWriteTime; } //compulsory check on file accessed and file created date. //IsFilePassedAgeLimit(fileAuditDate, fileAgeLimit) is depreceted if (IsFilePassedAgeLimit(currentFile.LastWriteTime, currentFile.LastAccessTime, fileAgeLimit)) { if (IsFileReadyForDelete(fullFilePath)) { if (DeleteFile(fullFilePath)) { currentFile.performedAction = ActionToPerform.FileDeleted; processedFile.AddFile(ActionToPerform.FileDeleted, currentFile); } else { currentFile.performedAction = ActionToPerform.FileFailedToDelete; processedFile.AddFile(ActionToPerform.FileFailedToDelete, currentFile); } } else { currentFile.performedAction = ActionToPerform.FileFailedToDelete; processedFile.AddFile(ActionToPerform.FileFailedToDelete, currentFile); } } else { currentFile.performedAction = ActionToPerform.FileForNextIteration; processedFile.AddFile(ActionToPerform.FileForNextIteration, currentFile); } } else { currentFile.performedAction = ActionToPerform.FileFailedToDelete; processedFile.AddFile(ActionToPerform.FileFailedToDelete, currentFile); } } return processedFile.Get(); }
/// <summary> /// <b>Description:</b> /// This will iterate the file system on the given path. /// Prepare the list of files which has passed fileAgeToValidate limit and to be /// deleted. /// Prepare the list of files which yet to pass fileAgeToValidate limit and to be /// stored. /// Check File is not open. /// Delete the file. /// if file has passed fileAgeToValidate and still cannot be deleted, mark file as /// failed to delete and record to database. /// Return the list. /// </summary> /// <param name="directoryLocation"></param> /// <param name="enumFileProperty"></param> /// <param name="fileAgeToValidate"></param> public ScannedFiles ScanFiles(string directoryLocation, FileDateProperty enumFileProperty, int fileAgeToValidate) { //create an array of files using FileInfo object FileInfo[] directoryFiles; //Create a Directory object using DirectoryInfo DirectoryInfo currentDirectory = new DirectoryInfo(directoryLocation); //get all files for the current directory directoryFiles = currentDirectory.GetFiles("*.*"); //iterate through the directory and print the files foreach (FileInfo file in directoryFiles) { ScannedFile scannedFileInfo = new ScannedFile(); //get details of each file using file object scannedFileInfo.Filename = file.FullName.Replace(file.DirectoryName+@"\", String.Empty); scannedFileInfo.Filesize = file.Length; scannedFileInfo.LastWriteTime = file.LastWriteTime; scannedFileInfo.LastAccessTime = file.LastAccessTime; scannedFileInfo.CreationTime = file.CreationTime; scannedFileInfo.Filepath = file.DirectoryName + @"\"; _filesToProcess.Add(scannedFileInfo); } //get sub-folders for the current directory DirectoryInfo[] subDirectories = currentDirectory.GetDirectories("*.*"); //This is the code that calls //the getDirsFiles (calls itself recursively) //This is also the stopping point //(End Condition) for this recursion function //as it loops through until //reaches the child folder and then stops. foreach (DirectoryInfo subDirectory in subDirectories) { ScanFiles(subDirectory.FullName, enumFileProperty, fileAgeToValidate); } return _filesToProcess; }
/// <summary> /// <b>Description:</b> /// With already scanned files to check, iterate through and validate each file. /// </summary> /// <param name="allowedAgeLimit">allowed age limit, in days, to check.</param> /// <param name="filesToCheck"></param> public ScannedFiles RunScan(int allowedAgeLimit, FileDateProperty fileProperty, ScannedFiles filesToCheck) { ScannedFiles cOfProcessedFiles = this.ProcessScannedFiles(allowedAgeLimit, fileProperty, filesToCheck); return cOfProcessedFiles; }
/// <summary> /// <b>Description:</b> /// With pass in allowed age limit on specified date access property on specific /// location, scan through the files and prepare for files to delete, and add it to /// collection of scanned files. /// </summary> /// <param name="allowedAgeLimit">allowed age limit, in days, to check.</param> /// <param name="fileDateProperty">allowed age limit, in days, to check on /// specified file date property. date created, accessed, modified.</param> /// <param name="directoryLocation">directory location to access.</param> public ScannedFiles RunScan(int allowedAgeLimit, FileDateProperty fileProperty, string directoryLocation) { ScannedFiles cOfScannedFiles = ScanFiles(directoryLocation, fileProperty, allowedAgeLimit); ScannedFiles cOfProcessedFiles = ProcessScannedFiles(allowedAgeLimit, fileProperty, cOfScannedFiles); return cOfProcessedFiles; }