Beispiel #1
0
        private void ProcessDirectories(SearchManager smgr, Scanner.ScanEngine s, string outFile, string errFile, bool imageScan, string[] files)
        {
            var starttime = DateTime.Now;

            // Print header
            PrintHeader(outFile, imageScan);

            // Process files
            var processedfiles = 0;

            foreach (var file in files)
            {
                try
                {
                    // If there is a file processor for a give file extension, process the file..
                    foreach (var fm in from fm in smgr.FileReaders where smgr.SupportFileExtension(fm, Path.GetExtension(file)) select fm)
                    {
                        // Only count if we have a file processor
                        ++processedfiles;

                        // Read the text
                        var fc = fm.ReadAllText(file, imageScan);

                        // Scan the text for patterns
                        s.Scan(fc.Text);

                        // Output start
                        PrintProcessingStart(outFile, s, file, fc, imageScan);

                        foreach (var match in s.PatternsFound.OrderBy(idx => idx.Index))
                        {
                            PrintMatch(outFile, match, imageScan);
                        }
                    }
                }
                catch (Exception err)
                {
                    File.AppendAllText(errFile, $"An error occured while processing: {file} => {err.Message}\n");
                }
            }
            if (processedfiles > 0)
            {
                PrintFooter(outFile, starttime, processedfiles, s.GetPatternNames());
            }
        }
Beispiel #2
0
        private async void BtnStart_Click(object sender, EventArgs e)
        {
            // Load the FileManagers
            var smgr = new SearchManager();

            smgr.ImportFileReaders();
            if (smgr.FileReaders.Count() == 0)
            {
                MessageBox.Show("No file readers found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Load the Scan engine and the patterns
            var s = new Scanner.ScanEngine();

            if (s.LoadPatterns() == 0)
            {
                MessageBox.Show("No patterns found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            btnStart.Enabled = false;
            btnClear.Enabled = false;
            foreach (ListViewItem i in lstBatch.Items)
            {
                if (i.SubItems[ListItemIdxStatus].Text == Waiting)
                {
                    var dir = i.SubItems[ListItemIdxDirectory].Text;

                    var outFile = dir + ".csv";
                    try
                    {
                        if (File.Exists(outFile))
                        {
                            File.Delete(outFile);
                        }
                    }
                    catch (Exception)
                    {
                        outFile = string.Format($"{dir} {DateTime.Now.Ticks}.csv");
                    }
                    string errFile = dir + ".err";
                    try
                    {
                        if (File.Exists(errFile))
                        {
                            File.Delete(errFile);
                        }
                    }
                    catch (Exception)
                    {
                        errFile = string.Format($"{dir} {DateTime.Now.Ticks}.err");
                    }

                    bool imageScan = (bool)i.SubItems[ListItemIdxImageScan].Tag;

                    // Load the files to be processed
                    var files = Directory.GetFiles(dir);
                    if (files.Length == 0)
                    {
                        File.AppendAllText(errFile, "No files found\n");
                    }

                    i.SubItems[ListItemIdxStatus].Text = "Processing";
                    await Task.Run(() => { ProcessDirectories(smgr, s, outFile, errFile, imageScan, files); });

                    i.SubItems[ListItemIdxStatus].Text = Processed;
                    i.Tag = outFile;
                }
            }
            btnClear.Enabled = true;
        }