public void Start()
        {
            Stop();
            var files = new List <FileInfo>();

            foreach (var f in Directory.GetFiles(configuration.Directory, configuration.FilenameFilter, SearchOption.TopDirectoryOnly))
            {
                files.Add(new FileInfo(f));
            }
            files.Sort((x, y) => x.CreationTime.CompareTo(y.CreationTime));

            for (int i = 0; i < files.Count - 1; i++)
            {
                oneFileProcessor.ProcessFile(files[i]);
            }
            if (files.Count > 0)
            {
                FileInfo lastFile = files[files.Count - 1];
                continuousFileProcessor.ChangeCurrentFile(lastFile.FullName);
            }
            // enable
            fileSystemWatcher = new FileSystemWatcher(configuration.Directory, configuration.FilenameFilter);
            fileSystemWatcher.IncludeSubdirectories = false;
            fileSystemWatcher.EnableRaisingEvents   = true;
            fileSystemWatcher.NotifyFilter          = NotifyFilters.FileName | NotifyFilters.LastWrite;
            fileSystemWatcher.Created += (x, y) => continuousFileProcessor.ChangeCurrentFile(y.FullPath);
            fileSystemWatcher.Renamed += (x, y) =>
            {
                if (y.FullPath.EndsWith(configuration.FilenameFilter.Replace("*", "")))
                {
                    continuousFileProcessor.ChangeCurrentFile(y.FullPath);
                }
            };
        }
Exemple #2
0
 public static void Main(string[] args)
 {
     try
     {
         //Task1 below has started as a prototype and hence been commented
         //Please uncomment the Task1 region to run the prototype/POC
         #region Task1 Prototype/Proof of Concept
         //if (FileProcessorPrototype.ProcessFile(args))
         //{
         //    Log.Debug("File Processing Completed Successfully");
         //    return;
         //}
         #endregion
         #region Task2 Complete File Processing
         if (FileProcessor.ProcessFile(args))
         {
             Log.Debug("File Processing Completed Successfully");
             return;
         }
         #endregion
         Log.Debug("Unable to Process the arguments");
     }
     catch (Exception exception)
     {
         Log.Debug("File Processing Failed");
         Log.Error(exception.Message);
         throw;
     }
 }
Exemple #3
0
 public void ProcessArchive(string PathToArchive, string DestinationPath)
 {
     try
     {
         var ArchiveFiles = ArchiveRetriever.RetrieveArchive(PathToArchive, DestinationPath, FileProcessor.ResolveRule);
         foreach (string PathToFile in ArchiveFiles)
         {
             FileProcessor.ProcessFile(PathToFile);
         }
     }catch (ArgumentException ex)
     {
         Console.WriteLine(ex.Message);
     }
     catch (FileNotFoundException ex)
     {
         Console.WriteLine(ex.Message);
     }
     catch (InvalidOperationException ex)
     {
         Console.WriteLine(ex.Message);
     }
     finally
     {
         ArchiveRetriever.DeleteArchive();
     }
 }
        public void FileProcessor_ProcessFile_Test()
        {
            // Given
            const string fileInputPath  = "Input Test Path";
            const string fileOutputPath = "Output Test Path";
            Canvas       canvas         = new Canvas(20, 4);
            List <Tuple <char, string[]> > parameters = new List <Tuple <char, string[]> >
            {
                Tuple.Create('C', new string[2] {
                    "20", "4"
                }),
                Tuple.Create('L', new string[4] {
                    "1", "2", "6", "2"
                }),
                Tuple.Create('R', new string[4] {
                    "16", "1", "20", "3"
                }),
                Tuple.Create('B', new string[3] {
                    "2", "3", "o"
                })
            };
            Line line = new Line(new Point(int.Parse(parameters[1].Item2[0]), int.Parse(parameters[1].Item2[1])),
                                 new Point(int.Parse(parameters[1].Item2[2]), int.Parse(parameters[1].Item2[3])));
            Rectangle rectangle = new Rectangle(new Point(int.Parse(parameters[2].Item2[0]), int.Parse(parameters[2].Item2[1])),
                                                new Point(int.Parse(parameters[2].Item2[2]), int.Parse(parameters[2].Item2[3])));

            _fileReader.Setup(fr => fr.ReadFile(fileInputPath)).Returns(parameters);
            var canvasParameter = parameters.FirstOrDefault(p => p.Item1 == 'C');

            _canvasService.Setup(cs => cs.CreateCanvas(int.Parse(canvasParameter.Item2[0]), int.Parse(canvasParameter.Item2[1]))).Returns(canvas);
            _fileWriter.Setup(fw => fw.ClearFile(fileOutputPath));
            _fileWriter.Setup(fw => fw.DrawShapeIntoFile(canvas, fileOutputPath));
            _lineService.Setup(ls => ls.CreateShape(ref canvas, line));
            _rectangleService.Setup(rs => rs.CreateShape(ref canvas, rectangle));
            _bucketFillGenerator.Setup(bf => bf.FillCanvas(ref canvas, new Point(int.Parse(parameters[3].Item2[0]), int.Parse(parameters[3].Item2[1])),
                                                           char.Parse(parameters[3].Item2[2])));
            // When
            var result = _fileProcessor.ProcessFile(fileInputPath, fileOutputPath);

            // Then
            Assert.IsTrue(result);
        }
Exemple #5
0
        private void SearchInternal(DirectoryInfo root, string pattern, Func <DirectoryInfo, string, IEnumerable <FileInfo> > listFunc)
        {
            StopCheck();
            try
            {
                foreach (var file in listFunc(root, pattern))
                {
                    StopCheck();
                    // Suppose short filename and creation timestamp concztenation is unique
                    var fileStamp = file.Name + file.CreationTime.Ticks;
                    if ((_proc == null || _proc.ProcessFile(file.FullName)) &&
                        !_foundFiles.Contains(fileStamp))
                    {
                        _foundFiles.Add(fileStamp);
                        FileFound(file.FullName);
                        Console.WriteLine("File found: {0}", file.FullName);
                    }
                }
                if (!_param.IsRecursive)
                {
                    return;
                }
                var dirs = root.EnumerateDirectories().Where(dir =>
                                                             _param.SearchInHiden || ((dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden));
                foreach (var dir in dirs)
                {
                    StopCheck();
                    // Suppose short filename and creation timestamp concztenation is unique
                    var dirStamp = dir.Name + dir.CreationTime.Ticks;
                    if (!_visitedPaths.Contains(dirStamp))
                    {
                        // Prevent step into rephase points more than once
                        if ((File.GetAttributes(dir.FullName) & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
                        {
                            _visitedPaths.Add(dirStamp);
                        }

                        SearchInternal(dir, pattern, listFunc);
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                Debug.WriteLine("Insufficient privileges: {0}", root.FullName);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("{0} : {1}", root.FullName, ex);
            }
        }
Exemple #6
0
 /// <summary>
 /// Apply a function to the files under a given directory and
 /// perhaps its subdirectories.
 /// </summary>
 /// <remarks>
 /// Apply a function to the files under a given directory and
 /// perhaps its subdirectories.  If the path is a directory then only
 /// files within the directory (perhaps recursively) that satisfy the
 /// filter are processed.  If the <code>path</code>is a file, then
 /// that file is processed regardless of whether it satisfies the
 /// filter.  (This semantics was adopted, since otherwise there was no
 /// easy way to go through all the files in a directory without
 /// descending recursively via the specification of a
 /// <code>FileFilter</code>.)
 /// </remarks>
 /// <param name="path">file or directory to load from</param>
 /// <param name="filter">
 /// a FileFilter of files to load.  The filter may be null,
 /// and then all files are processed.
 /// </param>
 /// <param name="processor">
 /// The <code>FileProcessor</code> to apply to each
 /// <code>File</code>
 /// </param>
 public static void ProcessPath(File path, IFileFilter filter, IFileProcessor processor)
 {
     if (path.IsDirectory())
     {
         // if path is a directory, look into it
         File[] directoryListing = path.ListFiles(filter);
         if (directoryListing == null)
         {
             throw new ArgumentException("Directory access problem for: " + path);
         }
         foreach (File file in directoryListing)
         {
             ProcessPath(file, filter, processor);
         }
     }
     else
     {
         // it's already passed the filter or was uniquely specified
         // if (filter.accept(path))
         processor.ProcessFile(path);
     }
 }
 public void ImportFile()
 {
     _fileReader.ReadFile();
     _mapper.Map();
     _fileProcessor.ProcessFile();
 }
        /// <summary>
        /// Does the actual work of iterating the folder tree.
        /// This is a breadth-first traversal.
        /// Code was adapted from the MSDN documentation for
        /// walking an NTFS directory.
        /// </summary>
        /// <param name="rootFolder">Folder to start the traversal from</param>
        private Collection <ProcessedFileInfo> BreadthFirstTraversal(String rootFolder)
        {
            // Data structure to hold names of subfolders to be
            // examined for files.
            Stack <string> dirs = new Stack <string>(20);

            if (System.IO.Directory.Exists(rootFolder))
            {
                //
                OnDirectoryWalkStarted(new DirectoryWalkEventArgs(rootFolder,
                                                                  String.Empty,
                                                                  DirectoryWalkObjectType.Directory,
                                                                  DirectoryWalkOperationResult.NotApplicable,
                                                                  "Directory walk started."));

                dirs.Push(rootFolder);
                FileProcessorLengthFirst lengthFirstFileProc    = new FileProcessorLengthFirst();
                IFileProcessor           fileProcessorInterface = lengthFirstFileProc as IFileProcessor;
                fileProcessorInterface.Initialize();
                while (dirs.Count > 0)
                {
                    string   currentDir = dirs.Pop();
                    string[] subDirs;
                    try
                    {
                        subDirs = System.IO.Directory.GetDirectories(currentDir);
                        OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                  String.Empty,
                                                                  DirectoryWalkObjectType.Directory,
                                                                  DirectoryWalkOperationResult.Success,
                                                                  "Directory listing (subdirectories) taken successfully."));
                    }
                    // An UnauthorizedAccessException exception will be thrown if we do not have
                    // discovery permission on a folder or file. It may or may not be acceptable
                    // to ignore the exception and continue enumerating the remaining files and
                    // folders. It is also possible (but unlikely) that a DirectoryNotFound exception
                    // will be raised. This will happen if currentDir has been deleted by
                    // another application or thread after our call to Directory.Exists. The
                    // choice of which exceptions to catch depends entirely on the specific task
                    // you are intending to perform and also on how much you know with certainty
                    // about the systems on which this code will run.
                    catch (UnauthorizedAccessException)
                    {
                        OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                  String.Empty,
                                                                  DirectoryWalkObjectType.Directory,
                                                                  DirectoryWalkOperationResult.Failure,
                                                                  "Directory listing (subdirectories) could not be taken - insufficient permissions."));
                        continue;
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                  String.Empty,
                                                                  DirectoryWalkObjectType.Directory,
                                                                  DirectoryWalkOperationResult.Error,
                                                                  "Directory listing (subdirectories) could not be taken - directory was deleted or moved."));
                        continue;
                    }

                    string[] files = null;
                    try
                    {
                        files = System.IO.Directory.GetFiles(currentDir);
                        OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                  String.Empty,
                                                                  DirectoryWalkObjectType.Directory,
                                                                  DirectoryWalkOperationResult.Success,
                                                                  "Directory listing (files) taken successfully."));
                    }

                    catch (UnauthorizedAccessException)
                    {
                        OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                  String.Empty,
                                                                  DirectoryWalkObjectType.Directory,
                                                                  DirectoryWalkOperationResult.Failure,
                                                                  "Directory listing (files) could not be taken - insufficient permissions."));
                        continue;
                    }

                    catch (System.IO.DirectoryNotFoundException)
                    {
                        OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                  String.Empty,
                                                                  DirectoryWalkObjectType.Directory,
                                                                  DirectoryWalkOperationResult.Error,
                                                                  "Directory listing (files) could not be taken - directory was deleted or moved."));
                        continue;
                    }
                    // Perform the required action on each file here.
                    // Modify this block to perform your required task.
                    foreach (string file in files)
                    {
                        try
                        {
                            OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                      Path.GetFileName(file),
                                                                      DirectoryWalkObjectType.File,
                                                                      DirectoryWalkOperationResult.NotApplicable,
                                                                      "Accessing file."));
                            System.IO.FileInfo fi = new System.IO.FileInfo(file);
                            fileProcessorInterface.ProcessFile(fi);
                            OnFileProcessed(new DirectoryWalkEventArgs(currentDir,
                                                                       Path.GetFileName(file),
                                                                       DirectoryWalkObjectType.File,
                                                                       DirectoryWalkOperationResult.Success,
                                                                       "File processed."));
                        }
                        catch (System.IO.FileNotFoundException)
                        {
                            // If file was deleted by a separate application
                            //  or thread since the call to TraverseTree()
                            // then just continue.
                            OnFileAccessed(new DirectoryWalkEventArgs(currentDir,
                                                                      String.Empty,
                                                                      DirectoryWalkObjectType.Directory,
                                                                      DirectoryWalkOperationResult.Error,
                                                                      "File could not be processed - it was deleted or moved."));
                            continue;
                        }
                    }

                    // Push the subdirectories onto the stack for traversal.
                    // This could also be done before handing the files.
                    foreach (string str in subDirs)
                    {
                        dirs.Push(str);
                    }
                }
                OnDirectoryWalkCompleted(new DirectoryWalkEventArgs(rootFolder,
                                                                    String.Empty,
                                                                    DirectoryWalkObjectType.Directory,
                                                                    DirectoryWalkOperationResult.NotApplicable,
                                                                    "Directory walk completed - computing hashes for length duplicates..."));
                Collection <ProcessedFileInfo> duplicateFiles = lengthFirstFileProc.DuplicateFiles;
                fileProcessorInterface.Close();
                return(duplicateFiles);
            }
            else
            {
                // Root folder does not exist
                throw new ArgumentException(String.Empty, "rootFolder");
            }
        } //BreadthFirstTraversal
        public void ensure_all_valid_versions_are_accepted()
        {
            var isValid = _fileProcessor.ProcessFile(validVersionArgs);

            Assert.IsTrue(isValid);
        }
        public DataSet ProcessFile(string filename, IFileProcessor fileProcessor)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if (fileProcessor == null)
            {
                throw new ArgumentNullException("fileProcessor");
            }

            var fullFileName = Path.Combine(this.unprocessedFilesPath, filename);

            if (!File.Exists(fullFileName))
            {
                throw new FileNotFoundException("File does not exist at location specified!", filename);
            }

            try
            {
                DataSet data = fileProcessor.ProcessFile(fullFileName);
                if (data != null && data.Tables.Count > 0)
                {
                    bool successfulMove = MoveFileToProcessedLocation(fullFileName);
                    if (successfulMove)
                    {
                        data.DataSetName = fullFileName;
                        Log.DebugFormat("Success");
                        return data;
                    }
                    else
                    {
                        Log.ErrorFormat("Unable to process file {0}", fullFileName);
                        return new DataSet();
                    }
                }
                else
                {
                    bool successfulMove = MoveFileToErrorLocation(fullFileName);
                    if (successfulMove)
                    {
                        data.DataSetName = fullFileName;
                        Log.DebugFormat("Success");
                        return data;
                    }
                    else
                    {
                        Log.ErrorFormat("Unable to process file {0}", fullFileName);
                        return new DataSet();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Unable to process file {0} {1}", filename, ex);
            }

            return new DataSet();
        }