[C#][IO][Threading][Example] Local data aggregation operation before work report
The data acquisition operation before the report is highly repetitive. The script is almost completed today. The following is the code:
1 // See https://aka.ms/new-console-template for more information 2 using System.IO; 3 using span> System.Threading; 4 5 string SN = @"C0230U25"; 6 string span> sourcefold = @"F: \Full picture"; 7 string span> flasefold = @"F: \Error"; 8 string span> rootPath = @""; 9 Report r = new(rootPath); 10 string span>[] foldlist = r.CreateFlaseFold(sourcefold, flasefold); 11 //Console.WriteLine(r.FindSN(sourcefold, SN)); 12 ThreadInvote tp = new ThreadInvote(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 span> (var fold in span> Directory.GetDirectories(@" F:\Error\1011")) 22 { 23 foreach span> (var file in span> Directory.GetFiles(fold, "*.jpg")) 24 //Console.WriteLine(file[(fold.Length+1)..]); 25 File.Copy(file, Path.Combine(@"F:\Error\1011 Overkill", 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 report data capture class 38 class span> Report 39 { 40 int span> count = 0; 41 string span> rootPath; 42 public span> Report(string rootPath) 43 { 44 this span>.rootPath = rootPath; 45 } 46 47 public string[] CreateFlaseFold(string sourcefold, string flasefold) 48 { 49 string span>[] foldlist = new string [4]; 50 string span> todaydate = Path.Combine(sourcefold, DateTime.Now.ToString("Yyyy year MM month dd day")); 51 foldlist[0 ] = todaydate; 52 string span> yestoday = Path.Combine(sourcefold, DateTime.Now.AddDays(-1).ToString("yyyy year MM month dd day")); 53 foldlist[1 ] = yestoday; 54 string span> foldnamepath = Path.Combine(flasefold, DateTime.Now.ToString("MMdd")); 55 foldlist[2 ] = foldnamepath; 56 string span> foldnamepath_ng = String.Concat(foldnamepath, "Overkill"); 57 foldlist[3 ] = foldnamepath_ng; 58 if span> (!Directory.Exists(foldnamepath)) 59 Directory.CreateDirectory (foldnamepath); 60 if span> (!Directory.Exists(foldnamepath_ng)) 61 Directory.CreateDirectory (foldnamepath_ng); 62 return span> foldlist; 63 } 64 65 public void CopyLimitFold(string[] foldlist, int sw) 66 { 67 int span> count = 0; 68 foreach span> (var item in span> 69 Directory.GetDirectories(Directory.GetDirectories(foldlist[sw])[0])) 70 { 71 int span> limitdate = Convert.ToInt32(String.Concat("1", Directory.GetCreationTime(item).ToString("HHmm"))); 72 string span> ffolders = Path.Combine(foldlist[2], item[30..]); 73 if span> (sw == 0 && limitdate >= 10000 && liba(0, 0, 255, 1)">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
In the above code, the key variables are in the top-level statements (SN, sourcefold, falsefold)
Due to business reasons, it is necessary to peel off multi-layer folders and accurately obtain all files in the specified folder and then copy them. Among them, simply personally feels that multiple Threads are repeatedly created in the main method. And it was troublesome to enable it, so I wrote a separate generic class to simplify the code in the main program.
Children who want to run the above program can create the corresponding sourcefold structure through the following Python code:
1 importos 2 3 # Root directory 4 root_path = r"C:\Users\Desktop\Practice" 5 6 for item in range(1, 41): 7 # span> Create 40 folders under root_path, 1-40 8 foldname = os.path.join (root_path, str(item)) 9 os.mkdir (foldname) 10 11 # Create another folder under each folder, 1-40, 1 12 foldname_son1 = os.path.join(foldname, str(item) + f'.{item}') 13 os.mkdir (foldname_son1) 14 15 # Create 500 more folders within 1-40, 1, 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 # Create a jpg image in each folder, and create an additional image in the 1 folder 21 pic_name = os.path.join(foldname_son1_son, str(item) + '.jpg') 22 if span> item == 1: 23 with open(pic_name[:-4] + '1.jpg', 'w'): 24 pass span> 25 with open(pic_name, 'w'): 26 pass span> 27 28 # Create a txt text under each file 29 txt_name = os.path.join(foldname_son1_son, str(item) + '.txt') 30 with open(txt_name, 'w '): 31 pass span>
8, 1)”>18 os.mkdir(foldname_son1_son)
19
20 # Create a jpg image in each folder, and create an additional image in the 1 folder
21 pic_name = os.path.join(foldname_son1_son, str(item) + ‘.jpg‘)
22 if span> item == 1:
23 with open(pic_name[:-4] + ‘1.jpg‘, ‘w‘):
24 pass span>
25 with open(pic_name, ‘w‘):
26 pass span>
27
28 # Create a txt text under each file
29 txt_name = os.path.join(foldname_son1_son, str(item) + ‘.txt‘)
30 with open(txt_name, ‘w ‘):
31 pass span>