using BaseFunction; using NSAnalysis.DAL; using System; using System.Data; using System.Data.SqlClient; using System.IO; namespace NSAnalysis.BaseUnit { public class FileSorter { public TMeasureMSSQLDAL tmdal = new TMeasureMSSQLDAL(); //定义读取的位置 public int readRowIndex = 2; // 默认读取第3行(从0开始计数) public int readColIndex = 1; // 默认读取第2列(从0开始计数) public FileSorter() { } 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(); string position = task["position"].ToString(); string matchStr = $"{modelCode}_{position}"; // 打印信息 MyBase.TraceWriteLine($"Processing Task - Source: {sourceDir}, Target: {targetDir}, ModelCode: {modelCode} Position: {position} matchStr: {matchStr} "); if (Directory.Exists(sourceDir)) { ProcessDirectory(sourceDir, targetDir, matchStr); } else { MyBase.TraceWriteLine($"Source directory does not exist: {sourceDir}"); } } } private DataTable GetTaskRecords() { SQLHelper.connStr = DatabaseDfn.SqlConnectStr(); DataTable dt = tmdal.SelectTaskByCondition("", "", "start"); //打印 dt PrintDataTable(dt); if (dt == null || dt.Rows.Count == 0) { MyBase.TraceWriteLine("No tasks found."); return null; } return dt; } private void ProcessDirectory(string sourceDir, string targetDir, string modelCode) { // 确保目标目录存在 if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); MyBase.TraceWriteLine($"Created target directory: {targetDir}"); } foreach (string file in Directory.GetFiles(sourceDir, "*.csv")) { // 打印正在处理的文件 MyBase.TraceWriteLine($"Processing file: {file}"); if (MatchCsvValue(file, modelCode, readRowIndex, readColIndex)) { string destFile = Path.Combine(targetDir, Path.GetFileName(file)); File.Move(file, destFile); MyBase.TraceWriteLine($"Moved: {file} -> {destFile}"); } else { MyBase.TraceWriteLine($"No match for file: {file}"); } } } /// /// 检查CSV文件中指定行列的字符串是否匹配目标值 /// /// CSV文件路径 /// 要匹配的目标字符串 /// 行索引(从0开始) /// 列索引(从0开始) /// 匹配成功返回true,否则false public static bool MatchCsvValue(string filePath, string targetValue, int rowIndex, int colIndex) { try { string[] lines = File.ReadAllLines(filePath); // 检查行索引是否有效 if (rowIndex < 0 || rowIndex >= lines.Length) return false; string[] columns = lines[rowIndex].Split(','); // 检查列索引是否有效 if (colIndex < 0 || colIndex >= columns.Length) return false; return columns[colIndex].Trim().Equals(targetValue); } catch (Exception ex) { MyBase.TraceWriteLine($"处理CSV文件时出错: {ex.Message}"); return false; } } public void test() { //SQLHelper.connStr = DatabaseDfn.SqlConnectStr(); //string testPath = @"D:\CJLR\DATA\Input\LLL\K0902906.csv"; //bool result = MatchCsvValue(testPath, "X540_L", 3, 1); //MyBase.TraceWriteLine($"匹配结果: {result}"); } // 编写一个打印 DataTable 对象的方法,输入是对象 public static void PrintDataTable(DataTable dt) { if (dt == null || dt.Rows.Count == 0) { MyBase.TraceWriteLine("DataTable is empty or null."); return; } foreach (DataColumn column in dt.Columns) { Console.Write($"{column.ColumnName}\t"); } MyBase.TraceWriteLine(""); foreach (DataRow row in dt.Rows) { foreach (var item in row.ItemArray) { Console.Write($"{item}\t"); } MyBase.TraceWriteLine(""); } } } }