【C#】【IO】【Threading】【实例】工作报表前的本地数据聚合操作
报表前的数据获取操作是高重复性的,今天差不多完成了脚本,下述是代码: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;
50 string todaydate = Path.Combine(sourcefold, DateTime.Now.ToString("yyyy年MM月dd日"));
51 foldlist = todaydate;
52 string yestoday = Path.Combine(sourcefold, DateTime.Now.AddDays(-1).ToString("yyyy年MM月dd日"));
53 foldlist = yestoday;
54 string foldnamepath = Path.Combine(flasefold, DateTime.Now.ToString("MMdd"));
55 foldlist = foldnamepath;
56 string foldnamepath_ng = String.Concat(foldnamepath, "过杀");
57 foldlist = 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)))
70 {
71 int limitdate = Convert.ToInt32(String.Concat("1", Directory.GetCreationTime(item).ToString("HHmm")));
72 string ffolders = Path.Combine(foldlist, item);
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, fileitem));
80 }
81 count++;
82 }
83 if (sw == 1 && limitdate >= 10800 || limitdate == 10000)
84 {
85 //File.Copy(item, Path.Combine(foldlist, item));
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, fileitem));
91 }
92 count++;
93 }
94 }
95 if (sw == 0)
96 Console.WriteLine($"{foldlist}:{count}");
97 if (sw == 1)
98 Console.WriteLine($"{foldlist}:{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)))
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
来源:https://www.cnblogs.com/vangoghpeng/archive/2023/10/11/17758243.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]