首页 > 编程语言 >【C#】【IO】【Threading】【实例】工作报表前的本地数据聚合操作

【C#】【IO】【Threading】【实例】工作报表前的本地数据聚合操作

时间:2023-10-11 21:34:15浏览次数:26  
标签:string item C# Threading sourcefold foldlist IO Directory foldname

<工作记录——Report>

报表前的数据获取操作是高重复性的,今天差不多完成了脚本,下述是代码:

  1 // See https://aka.ms/new-console-template for more information
  2 using System.IO;
  3 using System.Threading;
  4 
  5 string SN = @"C0230U25";
  6 string sourcefold = @"F:\整图图片";
  7 string flasefold = @"F:\报错";
  8 string rootPath = @"";
  9 Report r = new(rootPath);
 10 string[] foldlist = r.CreateFlaseFold(sourcefold, flasefold);
 11 //Console.WriteLine(r.FindSN(sourcefold, SN));
 12 ThreadInvote<Report> tp = new ThreadInvote<Report>(action, r, foldlist);
 13 
 14 static void action(Report r, string[] str, int num)
 15 {
 16     r.CopyLimitFold(str, num);
 17 }
 18 
 19 static void FindFalsePic()
 20 {
 21     foreach (var fold in Directory.GetDirectories(@"F:\报错\1011"))
 22     {
 23         foreach (var file in Directory.GetFiles(fold, "*.jpg"))
 24             //Console.WriteLine(file[(fold.Length+1)..]);
 25             File.Copy(file, Path.Combine(@"F:\报错\1011过杀", file[(fold.Length + 1)..]));
 26     }
 27 }
 28 
 29 //Thread t_0 = new Thread(() => r.CopyLimitFold(foldlist, 0));
 30 //Thread t_1 = new Thread(() => r.CopyLimitFold(foldlist, 1));
 31 //t_0.Start();
 32 //t_1.Start();
 33 //t_0.Join();
 34 //t_1.Join();
 35 
 36 
 37 # region 报表数据抓取类
 38 class Report
 39 {
 40     int count = 0;
 41     string rootPath;
 42     public Report(string rootPath)
 43     {
 44         this.rootPath = rootPath;
 45     }
 46 
 47     public string[] CreateFlaseFold(string sourcefold, string flasefold)
 48     {
 49         string[] foldlist = new string[4];
 50         string todaydate = Path.Combine(sourcefold, DateTime.Now.ToString("yyyy年MM月dd日"));
 51         foldlist[0] = todaydate;
 52         string yestoday = Path.Combine(sourcefold, DateTime.Now.AddDays(-1).ToString("yyyy年MM月dd日"));
 53         foldlist[1] = yestoday;
 54         string foldnamepath = Path.Combine(flasefold, DateTime.Now.ToString("MMdd"));
 55         foldlist[2] = foldnamepath;
 56         string foldnamepath_ng = String.Concat(foldnamepath, "过杀");
 57         foldlist[3] = foldnamepath_ng;
 58         if (!Directory.Exists(foldnamepath))
 59             Directory.CreateDirectory(foldnamepath);
 60         if (!Directory.Exists(foldnamepath_ng))
 61             Directory.CreateDirectory(foldnamepath_ng);
 62         return foldlist;
 63     }
 64 
 65     public void CopyLimitFold(string[] foldlist, int sw)
 66     {
 67         int count = 0;
 68         foreach (var item in
 69             Directory.GetDirectories(Directory.GetDirectories(foldlist[sw])[0]))
 70         {
 71             int limitdate = Convert.ToInt32(String.Concat("1", Directory.GetCreationTime(item).ToString("HHmm")));
 72             string ffolders = Path.Combine(foldlist[2], item[30..]);
 73             if (sw == 0 && limitdate >= 10000 && limitdate < 10800)
 74             {
 75                 if (!Directory.Exists(ffolders))
 76                     Directory.CreateDirectory(ffolders);
 77                 foreach (var fileitem in Directory.GetFiles(item))
 78                 {
 79                     File.Copy(fileitem, Path.Combine(foldlist[2], fileitem[30..]));
 80                 }
 81                 count++;
 82             }
 83             if (sw == 1 && limitdate >= 10800 || limitdate == 10000)
 84             {
 85                 //File.Copy(item, Path.Combine(foldlist[2], item[30..]));
 86                 if (!Directory.Exists(ffolders))
 87                     Directory.CreateDirectory(ffolders);
 88                 foreach (var fileitem in Directory.GetFiles(item))
 89                 {
 90                     File.Copy(fileitem, Path.Combine(foldlist[2], fileitem[30..]));
 91                 }
 92                 count++;
 93             }
 94         }
 95         if (sw == 0)
 96             Console.WriteLine($"{foldlist[sw]}:{count}");
 97         if (sw == 1)
 98             Console.WriteLine($"{foldlist[sw]}:{count}");
 99     }
100 
101     public int TotalFalseBoard()
102     {
103         foreach (var item in Directory.GetDirectories(rootPath))
104         {
105             string[] files = Directory.GetFiles(item, "*.jpg");
106             if (files.Length > 0)
107                 count++;
108         }
109         return count;
110     }
111 
112     public string FindSN(string sourcefold, string SN)
113     {
114         foreach (var item in Directory.GetDirectories(sourcefold))
115             foreach (var i_item in Directory.GetDirectories(Directory.GetDirectories(item)[0]))
116                 if (i_item.Contains(SN))
117                     return i_item;
118         return "NULL";
119     }
120 }
121 # endregion
122 
123 # region 多线程调用类
124 class ThreadInvote<T>
125 {
126     public ThreadInvote(Action<T, string[], int> action, T tt, string[] foldlist)
127     {
128         foreach (var item in Enumerable.Range(0, 2))
129         {
130             Thread t = new Thread((ThreadStart) => { action(tt, foldlist, item); });
131             t.Start();
132             t.Join();
133         }
134     }
135 }
136 #endregion
137 
138 
139 // Completed

上述代码中,关键变量分别都在顶级语句中(SN、sourcefold、falsefold)

因为业务原因,所以需要剥离多层文件夹,并精准获取指定文件夹中的所有文件然后进行拷贝,其中<多线程调用类>单纯个人觉得在主方法中重复性的创建多个Thread并启用觉得很麻烦,所以单独写了一个泛型类来简化主程序中的代码。

 

想要运行上述程序的童鞋可以通过下述Python代码来创建对应的sourcefold结构:

 1 import os
 2 
 3 # 根目录
 4 root_path = r"C:\Users\Desktop\Practice"
 5 
 6 for item in range(1, 41):
 7     # 在root_path下创建40个文件夹,1-40
 8     foldname = os.path.join(root_path, str(item))
 9     os.mkdir(foldname)
10 
11     # 每个文件夹下再创建一个文件夹,1-40,1
12     foldname_son1 = os.path.join(foldname, str(item) + f'.{item}')
13     os.mkdir(foldname_son1)
14 
15     # 再1-40,1内再创建500个文件夹,1-40,1,1-500
16     for i_item in range(1, 501):
17         foldname_son1_son = os.path.join(foldname_son1, str(i_item) + f'..{i_item}')
18         os.mkdir(foldname_son1_son)
19 
20         # 每个文件夹下创建一个jpg图片,并在1文件夹下多创建一张图
21         pic_name = os.path.join(foldname_son1_son, str(item) + '.jpg')
22         if item == 1:
23             with open(pic_name[:-4] + '1.jpg', 'w'):
24                 pass
25         with open(pic_name, 'w'):
26             pass
27 
28         # 每个文件下创建一个txt文本
29         txt_name = os.path.join(foldname_son1_son, str(item) + '.txt')
30         with open(txt_name, 'w'):
31             pass

 

标签:string,item,C#,Threading,sourcefold,foldlist,IO,Directory,foldname
From: https://www.cnblogs.com/vangoghpeng/p/17758243.html

相关文章

  • Effective C++ 笔记-1.1视C++为一个语言联邦
    应当这样正确的看待C++语言,将C++视为一个由相关次语言组成的联邦,而非单一语言。每种次语言都有自己的守则,当你从一个次语言移向另一个次语言,守则可能改变。其主要的次语言总共有4个:C:Object-OrientedC++;TemplateC++:STL:******C++高效编程守则视状况而变化,取决于你使用C+......
  • 无涯教程-ASP.NET Core - Razor表单
    在本章中,无涯教程将继续讨论标签助手,还将在应用程序中添加新功能,并使其能够编辑现有员工的详细信息,将在每个员工添加一个链接,该链接将转到HomeController上的Edit动作。@modelHomePageViewModel@{ViewBag.Title="Home";}<h1>Welcome!</h1><table>@forea......
  • cad标注文字太小,随比例改变设置 注释性; 文字太小
    标注文字太小命令:dimstyle——选中第1个——修改——调整——注释性打勾右下角:选择合适文字比例:文字太小双击文字,修改(高度),数值越大,越大......
  • [ARC128E] K Different Values
    [ARC128E]KDifferentValues考察\(k=2\)的情形,这个很经典,就是绝对众数。这样的话我们发现显然的一个必要条件是\(\maxA_i\le\lceil\frac{n}{k}\rceil\)。进一步,我们按照\(k\)为块长分块,还需满足\(A_i=\lceil\frac{n}{k}\rceil\)的个数不超过最后一段的块长。......
  • 在Mac上安装lxml
    最近想开始学习一下爬虫,用来截取一些网页中的段落文字、列表、表格等信息。联想到HTML的DOM树结构,就想是不是用XPath来解析会比较合适。于是自己想从Python结合XPath的方向入手来实现网页内容解析。提到Python与XPath结合,就要用到lxml这个包了。它是一款由StefanBehnel等开发者......
  • pytorch(8-6) 循环神经网络的简洁实现
    https://zh.d2l.ai/chapter_recurrent-neural-networks/rnn-concise.html API_85.pyimportcollectionsimportrefromd2limporttorchasd2limportrandomimportmathimporttorchimportrandomdraw_pic=0#@saved2l.DATA_HUB['time_machine']=......
  • Servlet.service() for servlet [dispatcherServlet] in context with path []
    一个不小心出现的错误,接口测试报500日志输出信息如下:[nio-8080-exec-2]o.a.c.c.C.[.[.[/].[dispatcherServlet]:Servlet.service()forservlet[dispatcherServlet]incontextwithpath[]threwexception[Requestprocessingfailed;nestedexceptionisjava.lang.Nu......
  • Web api整合EF Core
    注意这个是asp.netcore,asp.net出门左转首先vs创建或者rider创建新项目空的webapi项目,当前项目版本.NET6建表 添加依赖配置efcore使用nuget添加或者控制台 ,对应数据库和版本自己确定Microsoft.EntityFrameworkCore.SqlServers实体类映射配置和DbContext配置pub......
  • C++ - 单例模式实现
    1.什么是单例模式单例模式是指在整个系统生命周期内,保证一个类只能产生一个实例,确保该类的唯一性。为什么需要单例模式两个原因:节省资源。一个类只有一个实例,不存在多份实例,节省资源。方便控制。在一些操作公共资源的场景时,避免了多个对象引起的复杂操作。但是在实现单例......
  • 深入浅出JavaScript闭包
    什么是JS闭包?JS闭包是一个难点也是JS的特色,是JS的高级特性。首先我们知道JS运行函数的时候会在内存中开辟一个存储空间,会把函数体内的代码当作字符串一摸一样的放在这个空间中,把这个空间地址赋值给函数名(变量名),当我们调用函数的时候会根据地址找到这个储存空间,然后执行储存空......