c# - 移动包含字符串的文件,而不必随机生成文件路径
问题描述
我正在使用 ERP BEXT。BEXT 生成 4 种文件,必须在服务器上导入。所有文件名都包含一个特殊的 11 个字符代码,如下图所示。
4种文件的一个例子:
导入前,文件。我需要检查 4 文件是否存在具有相同的特殊 11 字符。这就是我所做的,我将特殊的 11 个字符代码存储在 .txt 文件中。
这是第一步,它工作正常。Atm,我将代码存储在字符串表中。我面临的当前问题是我必须随机生成文件的文件路径。它工作正常,但不是我想要的。出现一些错误,这就是我要优化它的原因。
我尝试使用 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();
}
}
推荐阅读
- reactjs - React / MaterialUI - 同名导入导出
- excel - 添加表行时出现运行时错误 1004
- mongodb - MongoDB:无法添加用户 - 未授权 (CloudFoundry)
- configuration - REP-501 无法连接到指定的数据库
- vue.js - 使用外部 json 填充路由器
- javascript - 我可以在没有任何循环的情况下计算我的 JSON 属性吗?
- reactjs - react-dropzone 子图标在状态更改时不更改
- r - 将单元格颜色映射到 kableExtra 中的数据值以创建热图表
- apache-kafka - Confluent Load Connector 无效 JSON - 解析错误:第 1 行第 7 列的数字文字无效
- c++ - 内存泄漏。如何正确使用删除?BN_bn2hex _CrtIsValidHeapPointer(块)