static int Main(string[] args) { MRArguments argument = UsageChecker.checkAndBuildArgument(args); if (MRStatusCode.CODE_SUCCESS != argument.statusCode) { return(argument.statusCode); } int processedCounter = 0; int totalCount = argument.targetFiles.Count; Parallel.ForEach(argument.targetFiles, (file) => { int currentProcessCount = Interlocked.Increment(ref processedCounter); MediaProcessingContext processingContext = new MediaProcessingContext(file); if (!processingContext.isSupportedMimeType) { Console.WriteLine("{2}/{3} - {0} is not supported file type [{1}]", processingContext.targetFile.Name, processingContext.mimeType, currentProcessCount, totalCount); return; } MediaFileService.checkOldestDateTime(processingContext); MediaFileService.createDestinationDir(argument, processingContext); MediaFileService.moveToDestination(processingContext); if (!processingContext.targetFile.FullName.Equals(processingContext.movedFile.FullName)) { MediaFileService.editFileDates(processingContext); Console.WriteLine("{3}/{4} - {0} -> {1} and set its all meta dates to [{2}]", processingContext.targetFile.Name, processingContext.movedFile.Name, processingContext.oldestFileDate, currentProcessCount, totalCount); return; } Console.WriteLine("{1}/{2} - {0} rearrangement failed.", processingContext.targetFile.Name, processingContext.movedFile.Name, processingContext.oldestFileDate, currentProcessCount, totalCount); }); return(MRStatusCode.CODE_SUCCESS); }
public static void createDestinationDir(MRArguments argument, MediaProcessingContext context) { String destinationParent = argument.dirForMove.FullName + Path.DirectorySeparatorChar + context.oldestFileDate.ToString("yyyyMMdd"); if (!File.Exists(destinationParent)) { Directory.CreateDirectory(destinationParent); } context.destinationParent = destinationParent; }
public static MRArguments checkAndBuildArgument(string[] args) { MRArguments baseResponse = new MRArguments { statusCode = MRStatusCode.CODE_SUCCESS }; if (args == null || args.Length < 2) { Console.WriteLine("Usage :media-files-rearrangement.exe {scan directory} {target directory}"); baseResponse.statusCode = MRStatusCode.CODE_INVALID_ARG_COUNT; return(baseResponse); } string scanDirectoryPath = args[0]; string targetDirectoryPath = args[1]; if (File.Exists(scanDirectoryPath) || !Directory.Exists(scanDirectoryPath)) { Console.WriteLine("src {0} is not exist or not directory", scanDirectoryPath); baseResponse.statusCode = MRStatusCode.CODE_INVALID_SCAN_DIR; return(baseResponse); } if (File.Exists(targetDirectoryPath) || !Directory.Exists(targetDirectoryPath)) { Console.WriteLine("target {0} is not exist or not directory", targetDirectoryPath); baseResponse.statusCode = MRStatusCode.CODE_INVALID_TARGET_DIR; return(baseResponse); } DirectoryInfo scanDirectory = new DirectoryInfo(scanDirectoryPath); Console.WriteLine("scan from {0}", scanDirectory.FullName); DirectoryInfo targetDirectory = new DirectoryInfo(targetDirectoryPath); Console.WriteLine("move to {0}", targetDirectory.FullName); FileInfo[] targetFiles = scanDirectory.GetFiles(); Console.WriteLine("target file count : {0}", targetFiles == null ? 0 : targetFiles.Length); List <FileInfo> files = new List <FileInfo>(targetFiles); baseResponse.dirForScan = scanDirectory; baseResponse.dirForMove = targetDirectory; baseResponse.targetFiles = files; return(baseResponse); }