public static void CreateFromDirectory(TaxonTreeNode _root, string path)
        {
            TaxonSearch searchTool    = new TaxonSearch(_root, true, true);
            int         countFound    = 0;
            int         countNotFound = 0;

            string[] files = Directory.GetFiles(path, "*.txt");

            string logFilename = Path.Combine(TaxonUtils.GetTaxonLocationPath(), "CreateFromDirectory.log");

            using (StreamWriter log = new StreamWriter(logFilename))
            {
                using (ProgressDialog progressDlg = new ProgressDialog())
                {
                    progressDlg.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                    progressDlg.Show();

                    ProgressItem parseFiles = progressDlg.Add("parseFiles", "", 0, files.Length);
                    foreach (string file in files)
                    {
                        parseFiles.Inc(file);
                        log.WriteLine("Import " + file + ":");
                        France.Departement dep = France.Data.Departements.GetDepartementFromName(Path.GetFileNameWithoutExtension(file));
                        if (dep == null)
                        {
                            log.WriteLine("  associated departement not found");
                            continue;
                        }

                        TaxonList.ImportFileResult resultImport = TaxonList.ImportFile(file, searchTool);
                        log.WriteLine("  " + resultImport.TaxonsFound + " taxons found");
                        log.WriteLine("  " + resultImport.TaxonNotFound + " taxons not found");

                        countFound    += resultImport.TaxonsFound;
                        countNotFound += resultImport.TaxonNotFound;

                        TaxonList taxons = new TaxonList();
                        taxons.FromTaxonTreeNodeList(resultImport.List);
                        taxons.HasFile  = true;
                        taxons.FileName = Path.Combine(TaxonUtils.GetTaxonLocationPath(), dep.Id + ".xml");
                        taxons.Save();
                    }
                }
            }

            string message = "Create location data from directory " + path + ": \n";

            message += String.Format("    taxons found: {0}\n", countFound);
            message += String.Format("    taxons not found: {0}\n", countNotFound);
            message += String.Format("for more details, look at " + logFilename + " file, and all other generated logs");
            Loggers.WriteInformation(LogTags.Location, message);
        }
Exemple #2
0
        private void ButtonImport_Click(object sender, EventArgs e)
        {
            string logFile = Path.Combine(TaxonUtils.GetLogPath(), "TagBatchImport.log");

            using (StreamWriter log = new StreamWriter(logFile))
            {
                string[] srcFiles = null;
                bool     error    = false;
                if (!Directory.Exists(textBoxSource.Text))
                {
                    log.WriteLine("Folder " + textBoxSource.Text + " does not exists");
                    error = true;
                }
                else
                {
                    try
                    {
                        srcFiles = Directory.GetFiles(textBoxSource.Text, "*.txt");
                        if (srcFiles.Length == 0)
                        {
                            log.WriteLine("Folder " + textBoxSource.Text + " contains no txt files");
                            error = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.WriteLine("Exception while getting list of txt file in folder " + textBoxSource.Text);
                        log.WriteLine("    " + ex.Message);
                        error = true;
                    }
                }

                if (!Directory.Exists(textBoxDestination.Text))
                {
                    log.WriteLine("Folder " + textBoxDestination.Text + " does not exists");
                    error = true;
                }

                if (error)
                {
                    log.WriteLine("Initialization error, stop batch import");
                    return;
                }

                bool overwriteAlways = radioButtonOverwrite.Checked;
                bool overwriteNever  = radioButtonLeaveIt.Checked;
                int  skippedFiles    = 0;
                int  errorFiles      = 0;

                foreach (string file in srcFiles)
                {
                    log.WriteLine();
                    log.WriteLine("--------------------------------------------------");
                    log.WriteLine("---> Import " + file);

                    string destinationFile = Path.Combine(textBoxDestination.Text, Path.GetFileNameWithoutExtension(file));
                    destinationFile = Path.ChangeExtension(destinationFile, ".lot");
                    if (File.Exists(destinationFile))
                    {
                        if (overwriteNever)
                        {
                            log.WriteLine("Import skipped, file " + destinationFile + " already exists");
                            skippedFiles++;
                            continue;
                        }
                        else if (!overwriteAlways)
                        {
                            DateTime datedest = File.GetLastWriteTime(destinationFile);
                            DateTime datesrc  = File.GetLastWriteTime(file);
                            if (datedest >= datesrc)
                            {
                                log.WriteLine("Import skipped, file " + destinationFile + " already exists and is newer than source file");
                                skippedFiles++;
                                continue;
                            }
                        }

                        try
                        {
                            File.Delete(destinationFile);
                        }
                        catch
                        {
                            log.WriteLine("Import skipped, file " + destinationFile + " already exists");
                            log.WriteLine("And got an error while trying to delete it");
                            errorFiles++;
                            continue;
                        }
                    }

                    while (_Searchtool == null)
                    {
                        Console.WriteLine("Wait search tool");
                        Thread.Sleep(100);
                    }

                    TaxonList.ImportFileResult result = TaxonList.ImportFile(file, _Searchtool, false);

                    TaxonList list = new TaxonList {
                        HasFile = true, FileName = destinationFile
                    };
                    list.FromTaxonTreeNodeList(result.List);

                    try
                    {
                        list.Save(false, TaxonList.FileFilterIndexEnum.ListOfTaxons);
                    }
                    catch (Exception ex)
                    {
                        log.WriteLine("Exception while saving list to " + destinationFile);
                        log.WriteLine("    " + ex.Message);
                        errorFiles++;
                        continue;
                    }

                    log.WriteLine(string.Format("    taxons found: {0}", result.TaxonsFound));
                    log.WriteLine(string.Format("    taxons not found: {0}", result.TaxonNotFound));
                    log.WriteLine(string.Format("for more details, look at " + result.LogFilename + " file"));
                    log.WriteLine(string.Format("==> Saved in " + destinationFile));
                }

                string message = "Batch import done,\n";
                message += string.Format("{0} total files scanned\n", srcFiles.Length);
                message += string.Format("{0} files skipped\n", skippedFiles);
                message += string.Format("{0} files not imported due to an error\n", errorFiles);
                message += string.Format("{0} files imported\n", srcFiles.Length - skippedFiles - errorFiles);
                message += string.Format("for more details, look at {0}", logFile);
                Loggers.WriteInformation(LogTags.Data, message);
                log.WriteLine("");
                log.WriteLine("==================================================");
                log.Write(message);
            }
        }