翼度科技»论坛 编程开发 .net 查看内容

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

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
报表前的数据获取操作是高重复性的,今天差不多完成了脚本,下述是代码:
  1.   1 // See https://aka.ms/new-console-template for more information
  2.   2 using System.IO;
  3.   3 using System.Threading;
  4.   4
  5.   5 string SN = @"C0230U25";
  6.   6 string sourcefold = @"F:\整图图片";
  7.   7 string flasefold = @"F:\报错";
  8.   8 string rootPath = @"";
  9.   9 Report r = new(rootPath);
  10. 10 string[] foldlist = r.CreateFlaseFold(sourcefold, flasefold);
  11. 11 //Console.WriteLine(r.FindSN(sourcefold, SN));
  12. 12 ThreadInvote<Report> tp = new ThreadInvote<Report>(action, r, foldlist);
  13. 13
  14. 14 static void action(Report r, string[] str, int num)
  15. 15 {
  16. 16     r.CopyLimitFold(str, num);
  17. 17 }
  18. 18
  19. 19 static void FindFalsePic()
  20. 20 {
  21. 21     foreach (var fold in Directory.GetDirectories(@"F:\报错\1011"))
  22. 22     {
  23. 23         foreach (var file in Directory.GetFiles(fold, "*.jpg"))
  24. 24             //Console.WriteLine(file[(fold.Length+1)..]);
  25. 25             File.Copy(file, Path.Combine(@"F:\报错\1011过杀", file[(fold.Length + 1)..]));
  26. 26     }
  27. 27 }
  28. 28
  29. 29 //Thread t_0 = new Thread(() => r.CopyLimitFold(foldlist, 0));
  30. 30 //Thread t_1 = new Thread(() => r.CopyLimitFold(foldlist, 1));
  31. 31 //t_0.Start();
  32. 32 //t_1.Start();
  33. 33 //t_0.Join();
  34. 34 //t_1.Join();
  35. 35
  36. 36
  37. 37 # region 报表数据抓取类
  38. 38 class Report
  39. 39 {
  40. 40     int count = 0;
  41. 41     string rootPath;
  42. 42     public Report(string rootPath)
  43. 43     {
  44. 44         this.rootPath = rootPath;
  45. 45     }
  46. 46
  47. 47     public string[] CreateFlaseFold(string sourcefold, string flasefold)
  48. 48     {
  49. 49         string[] foldlist = new string[4];
  50. 50         string todaydate = Path.Combine(sourcefold, DateTime.Now.ToString("yyyy年MM月dd日"));
  51. 51         foldlist[0] = todaydate;
  52. 52         string yestoday = Path.Combine(sourcefold, DateTime.Now.AddDays(-1).ToString("yyyy年MM月dd日"));
  53. 53         foldlist[1] = yestoday;
  54. 54         string foldnamepath = Path.Combine(flasefold, DateTime.Now.ToString("MMdd"));
  55. 55         foldlist[2] = foldnamepath;
  56. 56         string foldnamepath_ng = String.Concat(foldnamepath, "过杀");
  57. 57         foldlist[3] = foldnamepath_ng;
  58. 58         if (!Directory.Exists(foldnamepath))
  59. 59             Directory.CreateDirectory(foldnamepath);
  60. 60         if (!Directory.Exists(foldnamepath_ng))
  61. 61             Directory.CreateDirectory(foldnamepath_ng);
  62. 62         return foldlist;
  63. 63     }
  64. 64
  65. 65     public void CopyLimitFold(string[] foldlist, int sw)
  66. 66     {
  67. 67         int count = 0;
  68. 68         foreach (var item in
  69. 69             Directory.GetDirectories(Directory.GetDirectories(foldlist[sw])[0]))
  70. 70         {
  71. 71             int limitdate = Convert.ToInt32(String.Concat("1", Directory.GetCreationTime(item).ToString("HHmm")));
  72. 72             string ffolders = Path.Combine(foldlist[2], item[30..]);
  73. 73             if (sw == 0 && limitdate >= 10000 && limitdate < 10800)
  74. 74             {
  75. 75                 if (!Directory.Exists(ffolders))
  76. 76                     Directory.CreateDirectory(ffolders);
  77. 77                 foreach (var fileitem in Directory.GetFiles(item))
  78. 78                 {
  79. 79                     File.Copy(fileitem, Path.Combine(foldlist[2], fileitem[30..]));
  80. 80                 }
  81. 81                 count++;
  82. 82             }
  83. 83             if (sw == 1 && limitdate >= 10800 || limitdate == 10000)
  84. 84             {
  85. 85                 //File.Copy(item, Path.Combine(foldlist[2], item[30..]));
  86. 86                 if (!Directory.Exists(ffolders))
  87. 87                     Directory.CreateDirectory(ffolders);
  88. 88                 foreach (var fileitem in Directory.GetFiles(item))
  89. 89                 {
  90. 90                     File.Copy(fileitem, Path.Combine(foldlist[2], fileitem[30..]));
  91. 91                 }
  92. 92                 count++;
  93. 93             }
  94. 94         }
  95. 95         if (sw == 0)
  96. 96             Console.WriteLine($"{foldlist[sw]}:{count}");
  97. 97         if (sw == 1)
  98. 98             Console.WriteLine($"{foldlist[sw]}:{count}");
  99. 99     }
  100. 100
  101. 101     public int TotalFalseBoard()
  102. 102     {
  103. 103         foreach (var item in Directory.GetDirectories(rootPath))
  104. 104         {
  105. 105             string[] files = Directory.GetFiles(item, "*.jpg");
  106. 106             if (files.Length > 0)
  107. 107                 count++;
  108. 108         }
  109. 109         return count;
  110. 110     }
  111. 111
  112. 112     public string FindSN(string sourcefold, string SN)
  113. 113     {
  114. 114         foreach (var item in Directory.GetDirectories(sourcefold))
  115. 115             foreach (var i_item in Directory.GetDirectories(Directory.GetDirectories(item)[0]))
  116. 116                 if (i_item.Contains(SN))
  117. 117                     return i_item;
  118. 118         return "NULL";
  119. 119     }
  120. 120 }
  121. 121 # endregion
  122. 122
  123. 123 # region 多线程调用类
  124. 124 class ThreadInvote<T>
  125. 125 {
  126. 126     public ThreadInvote(Action<T, string[], int> action, T tt, string[] foldlist)
  127. 127     {
  128. 128         foreach (var item in Enumerable.Range(0, 2))
  129. 129         {
  130. 130             Thread t = new Thread((ThreadStart) => { action(tt, foldlist, item); });
  131. 131             t.Start();
  132. 132             t.Join();
  133. 133         }
  134. 134     }
  135. 135 }
  136. 136 #endregion
  137. 137
  138. 138
  139. 139 // Completed
复制代码
上述代码中,关键变量分别都在顶级语句中(SN、sourcefold、falsefold)
因为业务原因,所以需要剥离多层文件夹,并精准获取指定文件夹中的所有文件然后进行拷贝,其中单纯个人觉得在主方法中重复性的创建多个Thread并启用觉得很麻烦,所以单独写了一个泛型类来简化主程序中的代码。
 
想要运行上述程序的童鞋可以通过下述Python代码来创建对应的sourcefold结构:
  1. 1 import os
  2. 2
  3. 3 # 根目录
  4. 4 root_path = r"C:\Users\Desktop\Practice"
  5. 5
  6. 6 for item in range(1, 41):
  7. 7     # 在root_path下创建40个文件夹,1-40
  8. 8     foldname = os.path.join(root_path, str(item))
  9. 9     os.mkdir(foldname)
  10. 10
  11. 11     # 每个文件夹下再创建一个文件夹,1-40,1
  12. 12     foldname_son1 = os.path.join(foldname, str(item) + f'.{item}')
  13. 13     os.mkdir(foldname_son1)
  14. 14
  15. 15     # 再1-40,1内再创建500个文件夹,1-40,1,1-500
  16. 16     for i_item in range(1, 501):
  17. 17         foldname_son1_son = os.path.join(foldname_son1, str(i_item) + f'..{i_item}')
  18. 18         os.mkdir(foldname_son1_son)
  19. 19
  20. 20         # 每个文件夹下创建一个jpg图片,并在1文件夹下多创建一张图
  21. 21         pic_name = os.path.join(foldname_son1_son, str(item) + '.jpg')
  22. 22         if item == 1:
  23. 23             with open(pic_name[:-4] + '1.jpg', 'w'):
  24. 24                 pass
  25. 25         with open(pic_name, 'w'):
  26. 26             pass
  27. 27
  28. 28         # 每个文件下创建一个txt文本
  29. 29         txt_name = os.path.join(foldname_son1_son, str(item) + '.txt')
  30. 30         with open(txt_name, 'w'):
  31. 31             pass
复制代码
 

来源:https://www.cnblogs.com/vangoghpeng/archive/2023/10/11/17758243.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具