86 lines
1.9 KiB
C#
86 lines
1.9 KiB
C#
using System;
|
|
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.IO;
|
|
|
|
namespace NSAnalysis.BaseUnit
|
|
{
|
|
internal class FileSorter
|
|
{
|
|
private readonly string _connectionString;
|
|
|
|
public FileSorter(string connectionString)
|
|
{
|
|
_connectionString = connectionString;
|
|
}
|
|
|
|
public void ProcessFiles()
|
|
{
|
|
var tasks = GetTaskRecords();
|
|
foreach (DataRow task in tasks.Rows)
|
|
{
|
|
string sourceDir = task["sourceFile"].ToString();
|
|
string targetDir = task["targetFile"].ToString();
|
|
string modelCode = task["modelsCode"].ToString();
|
|
|
|
if (Directory.Exists(sourceDir))
|
|
{
|
|
ProcessDirectory(sourceDir, targetDir, modelCode);
|
|
}
|
|
}
|
|
}
|
|
|
|
private DataTable GetTaskRecords()
|
|
{
|
|
using (var connection = new SqlConnection(_connectionString))
|
|
{
|
|
var command = new SqlCommand(
|
|
"SELECT modelsCode, sourceFile, targetFile FROM CJLR_TASK_RELEASE WHERE status = 'start'",
|
|
connection);
|
|
|
|
var adapter = new SqlDataAdapter(command);
|
|
var dt = new DataTable();
|
|
adapter.Fill(dt);
|
|
return dt;
|
|
}
|
|
}
|
|
|
|
private void ProcessDirectory(string sourceDir, string targetDir, string modelCode)
|
|
{
|
|
if (!Directory.Exists(targetDir))
|
|
{
|
|
Directory.CreateDirectory(targetDir);
|
|
}
|
|
|
|
foreach (string file in Directory.GetFiles(sourceDir, "*.csv"))
|
|
{
|
|
if (FileContainsModelCode(file, modelCode))
|
|
{
|
|
string destFile = Path.Combine(targetDir, Path.GetFileName(file));
|
|
File.Move(file, destFile);
|
|
Console.WriteLine($"Moved: {file} -> {destFile}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool FileContainsModelCode(string filePath, string modelCode)
|
|
{
|
|
try
|
|
{
|
|
string content = File.ReadAllText(filePath);
|
|
return content.Contains(modelCode);
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//private static void Main(string[] args)
|
|
//{
|
|
// var sorter = new FileSorter("Your_Connection_String");
|
|
// sorter.ProcessFiles();
|
|
//}
|
|
}
|
|
} |