Ejemplo n.º 1
0
        public void Process()
        {
            WriteLine($"Begin process of {InputFilePath}");

            // Check if file exists
            if (!File.Exists(InputFilePath))
            {
                WriteLine($"ERROR: file {InputFilePath} does not exist.");
                return;
            }

            string rootDirectoryPath = new DirectoryInfo(InputFilePath).Parent.Parent.FullName;

            WriteLine($"Root data path is {rootDirectoryPath}");

            // Check if backup dir exists
            string inputFileDirectoryPath = Path.GetDirectoryName(InputFilePath);
            string backupDirectoryPath    = Path.Combine(rootDirectoryPath, BackupDirectoryName);

            // Doesn't Error if the Directory already exists
            Directory.CreateDirectory(backupDirectoryPath);

            // Copy file to backup dir
            string inputFileName  = Path.GetFileName(InputFilePath);
            string backupFilePath = Path.Combine(backupDirectoryPath, inputFileName);

            WriteLine($"Copying {InputFilePath}, to {backupFilePath}");
            File.Copy(InputFilePath, backupFilePath, true);

            // Move to in progress dir
            Directory.CreateDirectory(Path.Combine(rootDirectoryPath, InProgressDirectoryName));

            string inProgressFilePath = Path.Combine(rootDirectoryPath, InProgressDirectoryName, inputFileName);

            if (File.Exists(inProgressFilePath))
            {
                WriteLine($"ERROR: a file with the name {inProgressFilePath} is already being processed.");
                return;
            }

            WriteLine($"Moving {InputFilePath} to {inProgressFilePath}");
            File.Move(InputFilePath, inProgressFilePath);

            // Determine type of file
            string extension = Path.GetExtension(InputFilePath);

            string completedDirectoryPath = Path.Combine(rootDirectoryPath, CompletedDirectoryName);

            Directory.CreateDirectory(completedDirectoryPath);

            var completedFileName = $"{Path.GetFileNameWithoutExtension(InputFilePath)}-{Guid.NewGuid()}{extension}";

            var completedFilePath = Path.Combine(completedDirectoryPath, completedFileName);

            switch (extension)
            {
            case ".txt":
                var textProcessor = new TextFileProcessor(inProgressFilePath, completedFilePath);
                textProcessor.Process();
                break;

            case ".data":
                var binaryProcessor = new BinaryFileProcessor(inProgressFilePath, completedFilePath);
                binaryProcessor.Process();
                break;

            case ".csv":
                var csvProcessor = new CsvFileProcessor(inProgressFilePath, completedFilePath);
                csvProcessor.Process();
                break;

            default:
                WriteLine($"{extension} is an unsupported file type.");
                break;
            }

            WriteLine($"Completed processing of {inProgressFilePath}");
            WriteLine($"Deleting {inProgressFilePath}");
            File.Delete(inProgressFilePath);
        }
Ejemplo n.º 2
0
        public void Process()
        {
            Console.WriteLine($"Begining Single File Process of: {InputFilePath}" + "\r\n");

            //get root dir
            string rootDirectoryPath = new DirectoryInfo(InputFilePath).Parent.Parent.FullName;

            Console.WriteLine($"Root Directory Path is : {rootDirectoryPath}" + "\r\n");
            //check to see if file exists
            if (!File.Exists(InputFilePath))
            {
                Console.WriteLine($"ERROR: {InputFilePath} Does Not Exist.");
                return;
            }
            //check if backup dir exists
            string inputFileDirectoryPath = Path.GetDirectoryName(InputFilePath);
            string backupDirectoryPath    = Path.Combine(rootDirectoryPath, BackupDirectoryName);

            //check if backup dir exist if it doesnt exist create the dir
            if (!Directory.Exists(backupDirectoryPath))
            {
                Console.WriteLine($"Creating backup dir: {backupDirectoryPath}");
                Directory.CreateDirectory(backupDirectoryPath);
            }

            //copy the file in case processing errors
            string inputFileName  = Path.GetFileName(InputFilePath);
            string backupFilePath = Path.Combine(backupDirectoryPath, inputFileName);

            Console.WriteLine($"Copying {InputFilePath} to {backupFilePath}");

            File.Copy(InputFilePath, backupFilePath, true);


            //move to processing folder
            Directory.CreateDirectory(Path.Combine(rootDirectoryPath, InProgressDirectoryName));
            string inProgressFilePath = Path.Combine(rootDirectoryPath, InProgressDirectoryName, inputFileName);

            if (File.Exists(inProgressFilePath))
            {
                WriteLine($"ERROR: File with name {inProgressFilePath} already being processed!");
            }

            WriteLine($"Moving {InputFilePath} to {inProgressFilePath}");
            File.Move(InputFilePath, inProgressFilePath);



            //Determine the extension of the File
            string extension = Path.GetExtension(InputFilePath);


            string completedDirectoryPath = Path.Combine(rootDirectoryPath, CompletedDirectoryName);

            Directory.CreateDirectory(completedDirectoryPath);


            var completedFileName = $"{ Path.GetFileNameWithoutExtension(InputFilePath)}_{Guid.NewGuid()}{extension}";
            //change the extension
            //completedFileName = Path.ChangeExtension(completedFileName, ".complete");
            var completedFilePath = Path.Combine(completedDirectoryPath, completedFileName);



            //swithcing based on ext
            switch (extension)
            {
            case ".txt":
                var textFileProcessor = new TextFileProcessor(inProgressFilePath, completedFilePath);
                textFileProcessor.Process();
                break;

            case ".data":
                var binaryFileProcessor = new BinaryFileProcessor(inProgressFilePath, completedFilePath);
                binaryFileProcessor.Process();
                break;


            default:
                WriteLine($"{extension} is not supported");
                break;
            }



            //File.Move(inProgressFilePath, completedFilePath);
            //Console.WriteLine($"Processing Completed of {completedFilePath}");


            WriteLine($"Completed the processing of {inProgressFilePath}");
            WriteLine($"Deleting {inProgressFilePath}");
            File.Delete(inProgressFilePath);
        }
        internal void Process()
        {
            Console.WriteLine($"Begin process of {inputFilePath}");

            // Check if file exists
            if (!File.Exists(inputFilePath))
            {
                Console.WriteLine($"ERROR: file {inputFilePath} does not exist.");
                return;
            }

            string rootDirectoryPath = new DirectoryInfo(inputFilePath).Parent.Parent.FullName;

            Console.WriteLine($"Root data path is {rootDirectoryPath}");

            // Check if backup directory exists
            string inputFileDirectoryPath = Path.GetDirectoryName(inputFilePath);
            string backupDirectoryPath    = Path.Combine(rootDirectoryPath, BackupDirectoryName);

            //if (!Directory.Exists(backupDirectoryPath))
            //
            //    Console.WriteLine($"Creating {backupDirectoryPath}");

            //The method does not throw error if the directory already exists , no need to check
            Directory.CreateDirectory(backupDirectoryPath);

            // copy file to backup directory
            string inputFileName  = Path.GetFileName(inputFilePath);
            string backupFilePath = Path.Combine(backupDirectoryPath, inputFileName);

            Console.WriteLine($"Copying {inputFilePath} to {backupFilePath}");
            File.Copy(inputFilePath, backupFilePath, true);

            // Move to in progress dir
            Directory.CreateDirectory(Path.Combine(rootDirectoryPath, InProgressDirectoryName));
            string inProgressFilePath = Path.Combine(rootDirectoryPath, InProgressDirectoryName, inputFileName);

            if (File.Exists(inProgressFilePath))
            {
                Console.WriteLine($"ERROR: a file name {inProgressFilePath} is already being processed.");
                return;
            }

            Console.WriteLine($"Moving {inputFilePath} to {inProgressFilePath}");
            File.Move(inputFilePath, inProgressFilePath);

            // Determine type of file
            string extension = Path.GetExtension(inputFilePath);

            string completedDirectoryPath = Path.Combine(rootDirectoryPath, CompletedDirectoryName);

            Directory.CreateDirectory(completedDirectoryPath);

            var completedFileName = $"{Path.GetFileNameWithoutExtension(inputFilePath)}--{Guid.NewGuid()}-{extension}";


            var completedFilePath = Path.Combine(completedDirectoryPath, completedFileName);

            switch (extension)
            {
            case ".txt":
                //ProcessTxtFile(inProgressFilePath);
                var textProcessor = new TextFileProcessor(inProgressFilePath, completedFilePath);
                textProcessor.Process();
                break;

            case ".data":
                var binaryProcessor = new BinaryFileProcessor(inProgressFilePath, completedFilePath);
                binaryProcessor.Process();
                break;

            case ".csv":
                var csvProcessor = new CsvFileProcessor(inProgressFilePath, completedFilePath);
                csvProcessor.Process();
                break;

            default:
                Console.WriteLine($"{extension} is an unsupported file type.");
                break;
            }


            //File.Move(inProgressFilePath, completedFilePath);
            Console.WriteLine($"Completed processing of {inProgressFilePath}");
            Console.WriteLine($"Deleting {inProgressFilePath}");
            File.Delete(inProgressFilePath);
        }
Ejemplo n.º 4
0
        public void Process()
        {
            WriteLine($"Begin process of {InputFilePath}");

            // class: File class: http://bit.ly/psfileclass
            if (!File.Exists(InputFilePath))
            {
                WriteLine($"Error: file {InputFilePath} does not exist.");
                return;
            }

            // directory: find parent
            string rootDirectoryPath = new DirectoryInfo(InputFilePath).Parent.FullName;

            WriteLine($"Root data path is {rootDirectoryPath}");

            // directory: check directory exists
            // class: Path class: http://bit.ly/pspathclass
            string inputFileDirectoryPath = Path.GetDirectoryName(InputFilePath);
            string backupDirectoryPath    = Path.Combine(rootDirectoryPath, BackupDirectoryName);

            if (!Directory.Exists(backupDirectoryPath))
            {
                WriteLine($"Creating {backupDirectoryPath}");
                Directory.CreateDirectory(backupDirectoryPath);
            }

            // file: copying
            string inputFileName  = Path.GetFileName(InputFilePath);
            string backupFilePath = Path.Combine(backupDirectoryPath, inputFileName);

            WriteLine($"Copying {inputFileName} to {backupFilePath}");
            File.Copy(InputFilePath, backupFilePath, true); // overwrite true

            // file: moving
            Directory.CreateDirectory(Path.Combine(rootDirectoryPath, InProgressDirectoryName));
            string inProgressFilePath =
                Path.Combine(rootDirectoryPath, InProgressDirectoryName, inputFileName);

            if (File.Exists(inProgressFilePath))
            {
                WriteLine($"ERROR: a file with name {inProgressFilePath} is already being processed");
                return;
            }
            WriteLine($"Moving {InputFilePath} to {inProgressFilePath}");
            File.Move(InputFilePath, inProgressFilePath);

            // file: extension -> how to process, determine type of file
            string extension = Path.GetExtension(InputFilePath);

            string completedDirectoryPath = Path.Combine(rootDirectoryPath, CompletedDirectoryName);

            Directory.CreateDirectory(completedDirectoryPath);

            // WriteLine($"Moving {inProgressFilePath} to {completedDirectoryPath}");
            // File.Move(inProgressFilePath, Path.Combine(completedDirectoryPath, inputFileName));

            var completedFileName =
                $"{Path.GetFileNameWithoutExtension(InputFilePath)}-{Guid.NewGuid()}{extension}";

            completedFileName = Path.ChangeExtension(completedFileName, ".complete");

            var completedFilePath = Path.Combine(completedDirectoryPath, completedFileName);

            switch (extension)
            {
            case ".txt":
                var textProcessor = new TextFileProcessor(inProgressFilePath, completedFilePath);
                textProcessor.Process();
                break;

            case ".data":
                var binaryProcessor = new BinaryFileProcessor(inProgressFilePath, completedFilePath);
                binaryProcessor.Process();
                break;

            case ".csv":
                var csvFileProcessor = new CsvFileProcessor(inProgressFilePath, completedFilePath);
                csvFileProcessor.Process();
                break;

            default:
                WriteLine($"{extension} is an unsupported file type.");
                break;
            }

            WriteLine($"Completed processing of {inProgressFilePath}");

            WriteLine($"Deleting {inProgressFilePath}");
            File.Delete(inProgressFilePath);
        }
Ejemplo n.º 5
0
        public void Process()
        {
            Console.WriteLine($"Begin process of {InputFilePath}");

            if (!File.Exists(InputFilePath))
            {
                Console.WriteLine($"ERROR: file {InputFilePath} does not exist");
                return;
            }

            string rootDirectoryPath = new DirectoryInfo(InputFilePath).Parent.Parent.FullName;

            Console.WriteLine($"Root data path is {rootDirectoryPath}");

            string inputFileDirectoryPath = Path.GetDirectoryName(InputFilePath);

            Console.WriteLine($"inputFileDirectoryPath: {inputFileDirectoryPath}");
            string backupDirectoryPath = Path.Combine(rootDirectoryPath, BackupDirectoryName);

            Directory.CreateDirectory(backupDirectoryPath);

            string inputFileName  = Path.GetFileName(InputFilePath);
            string backupFilePath = Path.Combine(backupDirectoryPath, inputFileName);

            Console.WriteLine($"Copying {InputFilePath} to {backupFilePath}");
            File.Copy(InputFilePath, backupFilePath, true);

            Directory.CreateDirectory(Path.Combine(rootDirectoryPath, InProgressDirectoryName));
            string inProgressFilePath = Path.Combine(rootDirectoryPath, InProgressDirectoryName, inputFileName);

            if (File.Exists(inProgressFilePath))
            {
                Console.WriteLine($"ERROR: file with the name {inProgressFilePath} is already being processed");
                return;
            }

            Console.WriteLine($"Moving {InputFilePath} to {inProgressFilePath}");
            File.Move(InputFilePath, inProgressFilePath);

            string extension = Path.GetExtension(InputFilePath);

            string completedDirectoryPath = Path.Combine(rootDirectoryPath, CompletedDirectoryName);

            Directory.CreateDirectory(completedDirectoryPath);
            var completedFileName = $"{Path.GetFileNameWithoutExtension(InputFilePath)}-{Guid.NewGuid()}{extension}";
            var completedFilePath = Path.Combine(completedDirectoryPath, completedFileName);

            switch (extension)
            {
            case ".txt":
                var textProcessor = new TextFileProcessor(inProgressFilePath, completedFilePath);
                textProcessor.Process();
                break;

            case ".data":
                var binaryProcessor = new BinaryFileProcessor(inProgressFilePath, completedFilePath);
                binaryProcessor.Process();
                break;

            default:
                Console.WriteLine($"{extension} is an unsupported file type.");
                break;
            }

            Console.WriteLine($"Deleting {inProgressFilePath}");
            File.Delete(inProgressFilePath);
        }