ab56a9bcf7
SVN-Revision: r240
169 lines
8.3 KiB
C#
169 lines
8.3 KiB
C#
using Lskj.Core;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Lskj.GetFilesInDirectory
|
|
{
|
|
public partial class FrmMain : Form
|
|
{
|
|
public FrmMain()
|
|
{
|
|
InitializeComponent();
|
|
this.startBtn.Click += OnStartBtnClick;
|
|
}
|
|
/// <summary>
|
|
/// 流水号
|
|
/// </summary>
|
|
private int index = 1;
|
|
private void OnStartBtnClick(object sender, EventArgs e)
|
|
{
|
|
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
|
|
{
|
|
// 显示文件夹对话框
|
|
DialogResult result = folderBrowserDialog.ShowDialog();
|
|
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderBrowserDialog.SelectedPath))
|
|
{
|
|
this.pathBox.Text = folderBrowserDialog.SelectedPath;
|
|
if (DBConfig.Instance.CreateConnection())
|
|
{
|
|
SqlHelper.ExecuteNonQuery("if not exists (select top 1 * from sysObjects where Id=OBJECT_ID(N'P_FileSpeciesInfoTab') and xtype='U') begin CREATE TABLE [dbo].[P_FileSpeciesInfoTab]([fileid][int] IDENTITY(1, 1) NOT NULL,[sname][varchar](1000) NULL,[webpath][varchar](max) NULL,[filesize][int] NULL,[createtime][datetime] NULL,[creator][int] NULL,[employeename][varchar](100) NULL,[parentid][varchar](100) NULL,[pid][varchar](100) NULL,[speciesno][varchar](200) NULL) ON[PRIMARY] end");
|
|
//SqlHelper.ExecuteNonQuery("truncate table P_FileSpeciesInfoTab");
|
|
string folderPath = this.pathBox.Text;
|
|
DataTable speciesTable = SqlHelper.ExecuteDataTable("select SpeciesNo,SpeciesName,Ostr1,SUBSTRING(speciesno,1,len(speciesno)-2) as pid,FilePhase,FileStatus, bombillid, bombillno, PID, llevel, cpxh from p_FileArchivingSpeciesTab where 1 = 1");
|
|
string count = SqlHelper.ExecuteScalar("select count(fileid) from P_FileSpeciesInfoTab") + "";
|
|
int.TryParse(count, out index);
|
|
index += 1;//最大行号加1
|
|
TraverseDirectory(folderPath, speciesTable);
|
|
MessageBox.Show($"数据写入完成\r\n");
|
|
}
|
|
else
|
|
{
|
|
this.Invoke(new Action(() =>
|
|
{
|
|
this.msgBox.AppendText($"网络连接失败\r\n");
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取文件和匹配节点信息
|
|
/// </summary>
|
|
/// <param name="folderPath"></param>
|
|
/// <param name="speciesTable"></param>
|
|
private void TraverseDirectory(string folderPath, DataTable speciesTable)
|
|
{
|
|
try
|
|
{
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
// 获取当前文件夹中的所有文件
|
|
string[] files = Directory.GetFiles(folderPath);
|
|
foreach (string file in files)
|
|
{
|
|
try
|
|
{
|
|
string billNo = $"OLDGDWJ{DateTime.Now.ToString("yyMMdd")}{index.ToString().PadLeft(5, '0')}";
|
|
FileInfo fileInfo = new FileInfo(file);
|
|
string fileName = Path.GetFileName(file);
|
|
string[] infoArray = Path.GetFileNameWithoutExtension(fileName).Trim().Split(' ');
|
|
string filePath = Path.GetDirectoryName(file);
|
|
string dirName = Path.GetFileName(filePath);
|
|
string pattern = @".*fileroot";
|
|
filePath = Regex.Replace(filePath, pattern, "", RegexOptions.IgnoreCase).Trim('\\');
|
|
string webUrl = UrlEncode($"\\fileRoot\\{filePath}\\{fileName}");
|
|
string speciesNo = "";
|
|
if (!string.IsNullOrEmpty(filePath))
|
|
{
|
|
List<string> speciesList = filePath.Split('\\').ToList();
|
|
for (int i = 1; i <= speciesList.Count; i++)
|
|
{
|
|
List<DataRow> speciesRows = speciesTable.Select().Where(n => (n["SpeciesNo"] + "").Length == i * 2).ToList();
|
|
DataRow dataRow = speciesRows.Where(n => (n["speciesName"] + "").Equals(speciesList[i - 1])).FirstOrDefault();
|
|
if (dataRow != null)
|
|
{
|
|
speciesNo = dataRow["SpeciesNo"] + "";
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("不存在该目录结构");
|
|
}
|
|
}
|
|
}
|
|
DataTable dataTable = SqlHelper.ExecuteDataTable($"select * from P_FileSpeciesInfoTab where speciesno = '{speciesNo}' and sname = '{fileName}'");
|
|
if (dataTable != null && dataTable.Rows.Count > 0)
|
|
{
|
|
continue;
|
|
}
|
|
sqlBuilder.Append($"insert into p_FileArchiveFile (P_plmXLH,P_PlmFileStatus,P_PlmQXCP,Plm_Ldo_VeneerUsage,P_plmXLH01,SpeciesNo,P_plmCustom,P_PlmProductName,P_PlmKhBh,P_PlmYSCP,p_plmzcsj,P_PlmProductId,P_PlmBz,P_PlmFunctionModule,p_plmNbillno,p_plmVer) values ('{billNo}','4123','全新产品','{dirName}','{infoArray[0]}','{speciesNo}','{infoArray[3]}','{infoArray[1]}','{infoArray[4]}','112','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}','{Guid.NewGuid()}','老的型号附件批量上传','20241012','{infoArray[2]}','{infoArray.Last()}');\r\n");
|
|
sqlBuilder.Append($"insert into P_FileSpeciesInfoTab (sname,webpath,filesize,createtime,creator,employeename,ParentId,pid,speciesno) values ('{fileName}','{webUrl}','{fileInfo.Length}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}','112','袁利惠','0','{billNo}','{speciesNo}');\r\n");
|
|
index += 1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.Invoke(new Action(() =>
|
|
{
|
|
this.msgBox.AppendText($"文件:{file}获取信息失败:{ex.Message}\r\n");
|
|
}));
|
|
}
|
|
}
|
|
if (!string.IsNullOrEmpty(sqlBuilder.ToString()))
|
|
{
|
|
SqlHelper.ExecuteNonQuery(sqlBuilder.ToString());
|
|
}
|
|
// 获取当前文件夹中的所有子文件夹
|
|
string[] directories = Directory.GetDirectories(folderPath);
|
|
foreach (string directory in directories)
|
|
{
|
|
// 递归遍历子文件夹
|
|
TraverseDirectory(directory, speciesTable);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.Invoke(new Action(() =>
|
|
{
|
|
this.msgBox.AppendText($"{ex.Message}\r\n");
|
|
}));
|
|
}
|
|
}
|
|
public string UrlEncode(string con, bool cn = true, bool toPathStr = true)
|
|
{
|
|
string ret = con;
|
|
if (cn)
|
|
{
|
|
ret = HttpUtility.UrlEncode(con);
|
|
}
|
|
else
|
|
{
|
|
StringBuilder bder = new StringBuilder();
|
|
Regex HasCNReg = new Regex(@"[\u4e00-\u9fa5]");
|
|
for (int i = 0; i < con.Length; i++)
|
|
{
|
|
char c = con[1];
|
|
if (!HasCNReg.IsMatch(c.ToString()))
|
|
{
|
|
bder.Append(HttpUtility.UrlEncode(c.ToString()));
|
|
}
|
|
else
|
|
{
|
|
bder.Append(c);
|
|
}
|
|
}
|
|
ret = bder.ToString();
|
|
}
|
|
if (toPathStr)
|
|
{
|
|
return ret.Replace("%5c", "/").Replace("%2f", "/").Replace("//", "/");
|
|
}
|
|
return ret;
|
|
}
|
|
}
|
|
}
|