private void CopyFileToDest(string outputDirPath, RecupFile file) { var numberedDirectories = Directory.GetDirectories(outputDirPath) .Where(t => Directory.Exists(t)) .Select(t => Path.GetFileName(t)) .Where(t => DigitOnlyRegex.IsMatch(t)) .Select(t => int.Parse(t)) .OrderBy(t => t) .ToList(); string desiredDirectoryName = "0"; var desiredDirNameForThisFile = file.DesiredDirName.Value; if (desiredDirNameForThisFile != null) { desiredDirectoryName = desiredDirNameForThisFile; } else if (numberedDirectories.Any()) { var lastDir = Path.Combine(outputDirPath, numberedDirectories.Last().ToString()); var filesHere = Directory.GetFiles(lastDir); if (filesHere.Length >= 500) { desiredDirectoryName = (numberedDirectories.Last() + 1).ToString(); } else { desiredDirectoryName = numberedDirectories.Last().ToString(); } } var destDirPath = Path.Combine(outputDirPath, desiredDirectoryName); Directory.CreateDirectory(destDirPath); var sourceFilePath = Path.Combine(RootRecupDir, file.RelativePath); var destFilePath = Path.Combine(destDirPath, file.DesiredFileName.Value); if (FileActionType == FileActionType.Copy) { File.Copy(sourceFilePath, destFilePath); } else if (FileActionType == FileActionType.Move) { File.Move(sourceFilePath, destFilePath); } }
public List <RecupFile> FindFiles() { Console.WriteLine($"Handling root directory: {RootRecupDir}"); var recupDirs = GetRecupDirs(RootRecupDir).ToList(); var foundRecupFiles = new List <RecupFile>(); var lastNumber = recupDirs.LastOrDefault()?.Number; int totCount = 0; long totSize = 0; foreach (var recupDir in recupDirs) { var toPrint = $"Processing {recupDir.RecupDirName} / {lastNumber}"; Console.Write(toPrint); int count = 0; long size = 0; foreach (var file in Directory.GetFiles(recupDir.RecupDirPath)) { var recupFile = new RecupFile(RootRecupDir, file); foundRecupFiles.Add(recupFile); count++; size += recupFile.Size; } var padding = "".PadRight(Math.Max(1, 40 - toPrint.Length)); var toPrint2 = $"{padding}(Count: {count}, Size: {ValuesToStringHelper.BytesToString(size)})"; Console.Write(toPrint2); totCount += count; totSize += size; var padding2 = "".PadRight(Math.Max(1, 72 - (toPrint2.Length + toPrint.Length))); Console.WriteLine($"{padding2}(Total count: {totCount}, Total size: {ValuesToStringHelper.BytesToString(totSize)})"); } return(foundRecupFiles); }