1.需求:
前提:能够有整理数据表的条件
①数据清洗:从200G的图像包中跳出对应的店铺的图片
②数据导入失败后清洗出未正常导入的
GitHub:https://github.com/ArSang/C-.git
-1.需要手动处理的数据:文件的路径整理成一个Excel导入数据表中作为参数
use Images go CREATE TABLE [dbo].[ImageSouce]( [ImageSouce] [nvarchar](255) NULL ) ON [PRIMARY] GO
-2.文件来源:日志、数据库,格式如下:文件夹/文件名称
-3.将整理好的Excel导入[ImageSouce]表中,按照代码的app.config配置保存图片的地址执行程序即可
Program.cs
using log4net; using log4net.Config; using System; using System.Diagnostics; using System.IO; using System.Threading; using System.Data.SqlClient; using System.Data; using System.Configuration; namespace ImgCopFile { class Program { static void Main(string[] args) { // 监视代码运行时间 Stopwatch stopwatch = new Stopwatch(); // 开始监视 stopwatch.Start(); //定义logger var logger = LogManager.GetLogger(typeof(Program)); // 调用日志 InitLog4net(); logger.Info("-------- 程序Start! --------"); logger.Info("Start-DataTime:" + DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.TimeOfDay.ToString()); Thread.Sleep(3000); logger.InfoFormat("BuidImges:" + DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.TimeOfDay.ToString()); //Imgcop BuidImges(); string DirCount = ConfigurationManager.AppSettings["imagesDir"]; System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(DirCount); logger.Info("文件总数:" + GetFilesCount(dirInfo)); //delete //DeleteImges(); logger.Info("--------- 程序End!------------"); // 停止监视 stopwatch.Stop(); //获得当前实例测量得出的总时间 TimeSpan timeSpan = stopwatch.Elapsed; logger.Info("Stop-DataTime:" + DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.TimeOfDay.ToString()); logger.Info("SUM-DataTime:" + timeSpan); Console.ReadKey(); } #region ImgCop /// <summary> /// ImgCop - OpenSQL 图片COP /// </summary> public static void BuidImges() { string connectionStr = ConfigurationManager.ConnectionStrings["master"].ConnectionString; var log = LogManager.GetLogger(typeof(Program)); using (SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); DataTable ImageTable = new DataTable(); SqlDataAdapter Adapter = new SqlDataAdapter("SELECT * FROM ImageSouce", conn); lock (Adapter) { Adapter.Fill(ImageTable); } foreach (DataRow row in ImageTable.Rows) { //获取appSetings配置的key获取value值 string imgdate = ConfigurationManager.AppSettings["imagesRootDir"]; //图像复制后保存的地址 string imgdir = ConfigurationManager.AppSettings["imagesDir"]; string dest = imgdir + row["ImageSouce"].ToString(); //System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(imgdir); try { //创建复制后保存的文件夹 Directory.CreateDirectory(Path.GetDirectoryName(dest)); // 图片存放的原文件夹 File.Copy(imgdate + row["ImageSouce"].ToString(), dest); //剪切采用Move方法 log.InfoFormat(dest); //当前路径下的文件总数 //log.Info("总文件数:" + GetFilesCount(dirInfo)); } catch (Exception ex) { log.Info(ex.Message.ToString()); log.InfoFormat("Finished: FAILURE!"); } } conn.Close(); log.InfoFormat("Finished: SUCCESS!"); } } #endregion #region DeleteDir /// <summary> /// 根据数据库提供的表删除对应配置文件夹下的文件夹 /// </summary> public static void DeleteImges() { string connectionStr = ConfigurationManager.ConnectionStrings["master"].ConnectionString; var log = LogManager.GetLogger(typeof(Program)); using (SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); DataTable ImageTable = new DataTable(); SqlDataAdapter Adapter = new SqlDataAdapter("SELECT * FROM ImageSouce", conn); lock (Adapter) { Adapter.Fill(ImageTable); } foreach (DataRow row in ImageTable.Rows) { //获取appSetings配置的key获取value值 string imgdate = ConfigurationManager.AppSettings["DeleteDir"]; string deletedir = imgdate + row["ImageSouce"].ToString(); System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(imgdate); log.Info("要删除的文件总数:" + GetFilesCount(dirInfo)); try { string folderToBeDeleted = deletedir; DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted); if (folder.Exists) { folder.Delete(true); } } catch (Exception ex) { log.Info(ex.Message.ToString()); log.InfoFormat("Finished: FAILURE!"); } } conn.Close(); log.InfoFormat("Finished: SUCCESS!"); } } #endregion #region 获取当前目录下的文件总数 public static int GetFilesCount(System.IO.DirectoryInfo dirInfo) { int totalFile = 0; totalFile += dirInfo.GetFiles().Length; foreach (System.IO.DirectoryInfo subdir in dirInfo.GetDirectories()) { totalFile += GetFilesCount(subdir); } return totalFile; } #endregion #region 构造日志方法 /// <summary> /// 定义日志 /// </summary> private static void InitLog4net() { var log = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "Log4net.config"); XmlConfigurator.ConfigureAndWatch(log); } #endregion } }
Log4net.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections> <log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <!--日志路径--> <param name= "File" value= "Log\"/> <!--是否是向文件中追加日志--> <param name= "AppendToFile" value= "true"/> <!--log保留天数--> <param name= "MaxSizeRollBackups" value= "10"/> <!--日志文件名是否是固定不变的--> <param name= "StaticLogFileName" value= "false"/> <!--日志文件名格式为:2008-08-31.log--> <param name= "DatePattern" value= "yyyy-MM-dd".log""/> <!--日志根据日期滚动--> <param name= "RollingStyle" value= "Date"/> <layout type="log4net.Layout.PatternLayout"> <!--<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />--> <!--日志输出打印样式--> <param name="ConversionPattern" value="%d -[%-5p] %m%n" /> </layout> </appender> <!-- 控制台前台显示日志 --> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="Info" /> <foreColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <!--<conversionPattern value="%n%date{HH:mm:ss,fff} [%-5level] %m" />--> <conversionPattern value="%n%d[%-5level] %m" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Info" /> <param name="LevelMax" value="Fatal" /> </filter> </appender> <root> <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) --> <level value="all" /> <appender-ref ref="ColoredConsoleAppender"/> <appender-ref ref="RollingLogFileAppender"/> </root> </log4net> </configuration>
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup> <appSettings> <!--图像原文件地址--> <add key="imagesRootDir" value="D:\FTPdata\" /> <!--图像复制后保存地址--> <add key="imagesDir" value="D:\imagesFailed\" /> <!--文件夹删除路径配置--> <add key="DeleteDir" value="D:\imagesFailed\" /> </appSettings> <connectionStrings> <add name="master" connectionString="Server=.;Database=ImageYQ;User Id=sa;Password=123456;Connection Timeout=3000;" /> </connectionStrings> </configuration>