无需随机生成文件路径即可移动包含字符串的文件

Move Files containing a String without having to randomly generate the Filepath

我正在使用 ERP BEXT。 BEXT 生成 4 种文件,必须在服务器上导入。所有文件名都包含一个特殊的 11 字符代码,如下图所示。

4种文件的例子:

导入前,文件。我需要检查 4 文件是否存在具有相同的特殊 11 字符。 这就是我所做的,我将特殊的 11 个字符代码存储在 .txt 文件中。

这是第一步,效果很好。 Atm,我将代码存储在字符串的 table 中。 我目前面临的问题是我必须随机生成文件的文件路径。 它工作正常,但不是我想要的。出现了一些错误,所以我想优化它。

我试过使用 try 并捕获异常,并避免错误,但它并不干净。而且程序有时会崩溃。

string ID = File.ReadAllText(@"\srv-bext\import-bext\acontrol.txt");
                string prefix1 = "exp_vente_ent_";
                string prefix3 = "exp_vente_lig_";
                string prefix5 = "rcp_achat_ent_";
                string prefix6 = "rcp_achat_lig_";
                string prefix7 = "rec_vente_lig_";
                string prefix8 = "rec_vente_ent_";
                string a = ".txt";
                string b = ".top";
                String root = @"\srv-bext\import-bext\";
                String[] lines = System.IO.File.ReadAllLines(@"\srv-bext\import-bext\acontrol.txt");

                for (int i = 0; i <= lines.Length; i++)
                {
                    try
                    {
                        //génération de filepath ciblé, pour trouver les bons et bouger les fichiers
                        string fullPath0 = System.IO.Path.Combine(root, prefix1 + lines[i] + a);
                        string fullPath0a = System.IO.Path.Combine(root, prefix1 + lines[i] + b);
                        string fullPath2 = System.IO.Path.Combine(root, prefix3 + lines[i] + a);
                        string fullPath2a = System.IO.Path.Combine(root, prefix3 + lines[i] + b);
                        string fullPath4 = System.IO.Path.Combine(root, prefix5 + lines[i] + a);
                        string fullPath4a = System.IO.Path.Combine(root, prefix5 + lines[i] + b);
                        string fullPath5 = System.IO.Path.Combine(root, prefix6 + lines[i] + a);
                        string fullPath5a = System.IO.Path.Combine(root, prefix6 + lines[i] + b);
                        string fullPath6 = System.IO.Path.Combine(root, prefix7 + lines[i] + a);
                        string fullPath6a = System.IO.Path.Combine(root, prefix7 + lines[i] + b);
                        string fullPath7 = System.IO.Path.Combine(root, prefix8 + lines[i] + a);
                        string fullPath7a = System.IO.Path.Combine(root, prefix8 + lines[i] + b);



                        if (File.Exists(fullPath0))
                        {
                            try
                            {


                                File.Copy(fullPath0, @"\srv-bext\ARCHIVES-PROD\" + fullPath0.Substring(23, 29));
                                File.Move(fullPath0, @"\vm-erp-sql\Interfaces_BEXT\PROD\Import\" + fullPath0.Substring(23, 29));

                            }
                            catch
                            {
                                Console.WriteLine("Checked");
                            }
                        }

                        if (File.Exists(fullPath0a))

                        {

                            try
                            {

                                File.Copy(fullPath0a, @"\srv-bext\ARCHIVES-PROD\" + fullPath0a.Substring(23, 29));
                                File.Move(fullPath0a, @"\vm-erp-sql\Interfaces_BEXT\PROD\Import\" + fullPath0a.Substring(23, 29));



                            }
                            catch
                            {
                                Console.WriteLine("Not done");
                            }

我只想移动包含 11 个字符代码的文件,而不必随机生成文件路径。

更简单的东西:

class Program
{
    static void Main(string[] args)
    {
        string input_dir = @"env\srv-bext\import-bext\";
        string output_dir = @"env\srv-bext\import-bext\imported\";
        string archive_dir = @"env\srv-bext\import-bext\archive\";
        string[] index = File.ReadAllLines(input_dir + "acontrol.txt");
        string[] prefixes = new string[] {
            "exp_vente_ent_",
            "ctl_",
            "exp_vente_lig_",
            "mvt_",
            "rcp_achat_ent_",
            "rcp_achat_lig_",
            "rec_vente_lig_",
            "rec_vente_ent_"
        };
        string[] extensions = new string[2] {"txt", "top" };

        if (!Directory.Exists(input_dir))
        {
            Console.WriteLine("Err: Input directory doesn't exist.");
            Console.ReadLine();
            return;
        }
        if (!Directory.Exists(output_dir))
            Directory.CreateDirectory(output_dir);
        if (!Directory.Exists(archive_dir))
            Directory.CreateDirectory(archive_dir);

        foreach (string id  in index)
        {
            foreach (string pre in prefixes)
            {
                foreach (string ext in extensions)
                {
                    string filename = pre + id + "." + ext;

                    if (File.Exists(input_dir + filename))
                    {
                        try
                        {
                            File.Copy(input_dir + filename, output_dir + filename);
                            File.Copy(input_dir + filename, archive_dir + filename);
                            File.Delete(input_dir + filename);
                            Console.WriteLine("Info: " + filename + " file processed");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Err: " + e.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Warn: " + filename + " is missing");
                    }
                }
            }
        }
        Console.ReadLine();
    }
}