Browse Source

修改文件转换

callm 5 năm trước cách đây
mục cha
commit
47a28b12a1
2 tập tin đã thay đổi với 131 bổ sung33 xóa
  1. 28 2
      FileWatcher/Form1.Designer.cs
  2. 103 31
      FileWatcher/Form1.cs

+ 28 - 2
FileWatcher/Form1.Designer.cs

@@ -33,6 +33,8 @@
             this.richTextBox1 = new System.Windows.Forms.RichTextBox();
             this.timer1 = new System.Windows.Forms.Timer(this.components);
             this.timer2 = new System.Windows.Forms.Timer(this.components);
+            this.button1 = new System.Windows.Forms.Button();
+            this.textBox1 = new System.Windows.Forms.TextBox();
             ((System.ComponentModel.ISupportInitialize)(this.FileWatcher)).BeginInit();
             this.SuspendLayout();
             // 
@@ -50,7 +52,7 @@
             this.richTextBox1.Location = new System.Drawing.Point(24, 24);
             this.richTextBox1.Margin = new System.Windows.Forms.Padding(6);
             this.richTextBox1.Name = "richTextBox1";
-            this.richTextBox1.Size = new System.Drawing.Size(516, 444);
+            this.richTextBox1.Size = new System.Drawing.Size(1350, 778);
             this.richTextBox1.TabIndex = 0;
             this.richTextBox1.Text = "";
             // 
@@ -62,11 +64,32 @@
             // 
             this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
             // 
+            // button1
+            // 
+            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
+            this.button1.Location = new System.Drawing.Point(1259, 809);
+            this.button1.Name = "button1";
+            this.button1.Size = new System.Drawing.Size(115, 40);
+            this.button1.TabIndex = 1;
+            this.button1.Text = "解析";
+            this.button1.UseVisualStyleBackColor = true;
+            this.button1.Click += new System.EventHandler(this.button1_Click);
+            // 
+            // textBox1
+            // 
+            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
+            this.textBox1.Location = new System.Drawing.Point(24, 811);
+            this.textBox1.Name = "textBox1";
+            this.textBox1.Size = new System.Drawing.Size(1229, 35);
+            this.textBox1.TabIndex = 2;
+            // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(568, 522);
+            this.ClientSize = new System.Drawing.Size(1402, 856);
+            this.Controls.Add(this.textBox1);
+            this.Controls.Add(this.button1);
             this.Controls.Add(this.richTextBox1);
             this.Margin = new System.Windows.Forms.Padding(6);
             this.Name = "Form1";
@@ -74,6 +97,7 @@
             this.Load += new System.EventHandler(this.Form1_Load);
             ((System.ComponentModel.ISupportInitialize)(this.FileWatcher)).EndInit();
             this.ResumeLayout(false);
+            this.PerformLayout();
 
         }
 
@@ -83,6 +107,8 @@
         private System.Windows.Forms.RichTextBox richTextBox1;
         private System.Windows.Forms.Timer timer1;
         private System.Windows.Forms.Timer timer2;
+        private System.Windows.Forms.Button button1;
+        private System.Windows.Forms.TextBox textBox1;
     }
 }
 

+ 103 - 31
FileWatcher/Form1.cs

@@ -33,7 +33,6 @@ namespace FileWatcher
 
             timer2.Interval = 1000 * 60 * 60;
             timer2.Start();
-            RunTran();
         }
 
         private void Watcher_Created(object sender, FileSystemEventArgs e)
@@ -45,6 +44,7 @@ namespace FileWatcher
                     string FullName = e.FullPath;
                     string Filename = FullName.Substring(FullName.LastIndexOf(@"\") + 1).Split('.')[0];
                     string StartPath = FullName.Substring(0, FullName.LastIndexOf(@"\") + 1);
+                    Thread.Sleep(1500);
                     if (File.Exists(FullName))
                     {
                         richTextBox1.AppendText(DateTime.Now.ToString("yyyy/MM/dd h:mm:ss.fff") + " " + e.FullPath + "\n");
@@ -54,8 +54,8 @@ namespace FileWatcher
                     {
                         richTextBox1.AppendText("不存在文件" + FullName + "\n");
                     }
-                    //不存在同名的文件则进行转换
-                    if (!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName))
+                    //不存在同名的文件则进行转换,或者Excel文件小于100KB的
+                    if ((!File.Exists(StartPath + Filename + ".xls") && File.Exists(FullName)))
                     {
                         if (FullName.Substring(FullName.LastIndexOf(".") + 1) == "jdf")
                         {
@@ -160,24 +160,31 @@ namespace FileWatcher
 
         private void timer1_Tick(object sender, EventArgs e)
         {
-            Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
-            for (int i = 0; i < processes1.Length; i++)
+            try
             {
-                //杀掉超过20秒没关闭的进程
-                if (DateTime.Now > processes1[1].StartTime.AddSeconds(20))
+                Process[] processes1 = Process.GetProcessesByName("DTS-JDFData2Excel");
+                for (int i = 0; i < processes1.Length; i++)
                 {
-                    processes1[i].Kill();
+                    //杀掉超过20秒没关闭的进程
+                    if (DateTime.Now > processes1[1].StartTime.AddSeconds(60))
+                    {
+                        processes1[i].Kill();
+                    }
                 }
-            }
 
-            Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel");
-            for (int i = 0; i < processes2.Length; i++)
-            {
-                if (DateTime.Now > processes2[1].StartTime.AddSeconds(20))
+                Process[] processes2 = Process.GetProcessesByName("DTS-Data2Excel");
+                for (int i = 0; i < processes2.Length; i++)
                 {
-                    processes2[i].Kill();
+                    if (DateTime.Now > processes2[1].StartTime.AddSeconds(60))
+                    {
+                        processes2[i].Kill();
+                    }
                 }
             }
+            catch (Exception)
+            {
+
+            }
         }
 
         private void RunTran()
@@ -185,22 +192,20 @@ namespace FileWatcher
             try
             {
                 List<FileInfo> lst = new List<FileInfo>();
-                string[] path = new string[] { @"D:\PROB-001", @"D:\PROB-002", @"D:\PROB-003", @"D:\PROB-004",
-                @"D:\PROB-005", @"D:\PROB-006", @"D:\PROB-007", @"D:\PROB-008", @"D:\PROB-009",
-                @"D:\PROB-010", @"D:\PROB-011", @"D:\PROB-012", @"D:\PROB-013", @"D:\PROB-014",
-                @"D:\PROB-015", @"D:\PROB-004", @"D:\PROB-016", @"D:\PROB-017", @"D:\PROB-018",
-                @"D:\PROB-019", @"D:\PROB-020", @"D:\PROB-021", @"D:\PROB-022", @"D:\PROB-023",
-                @"D:\PROB-024", @"D:\PROB-026",@"D:\PROB-027",@"D:\PROB-028",@"D:\PROB-029",
-                @"D:\PROB-030",@"D:\PROB-031",@"D:\PROB-032",@"D:\PROB-033",@"D:\PROB-034",
-                @"D:\PROB-035",@"D:\PROB-036",@"D:\PROB-037",@"D:\PROB-038",@"D:\PROB-039",
-            @"D:\PROB-040",@"D:\PROB-041",@"D:\PROB-042",@"D:\PROB-043",@"D:\PROB-045",@"D:\PROB-046",
-            @"D:\PROB-047",@"D:\PROB-048",@"D:\PROB-049",@"D:\PROB-050",@"D:\PROB-051",@"D:\PROB-052",@"D:\PROB-053",
-            @"D:\PROB-054",@"D:\PROB-055",@"D:\PROB-056",@"D:\PROB-057",@"D:\PROB-058",@"D:\PROB-059",@"D:\PROB-060",
-            @"D:\PROB-061",@"D:\PROB-062",@"D:\PROB-063",@"D:\PROB-064",@"D:\PROB-65",@"D:\PROB-66",@"D:\PROB-067",
-            @"D:\PROB-068",@"D:\PROB-069",@"D:\PROB-070",@"D:\PROB-071",@"D:\PROB-072",@"D:\PROB-073",@"D:\PROB-074",
-            @"D:\PROB-075",@"D:\PROB-76",@"D:\PROB-077",@"D:\PROB-078",@"D:\PROB-079",@"D:\PROB-080",@"D:\PROB-081",@"D:\PROB-082"};
-
-                //string[] path = textBox1.Text.Split(',');
+                string[] path = new string[] { @"D:\PROB-071",@"D:\PROB-072",@"D:\PROB-073",@"D:\PROB-074",
+                @"D:\PROB-075",@"D:\PROB-76",@"D:\PROB-077",@"D:\PROB-078",@"D:\PROB-079",@"D:\PROB-080",@"D:\PROB-081",@"D:\PROB-082",@"D:\PROB-001", @"D:\PROB-002", @"D:\PROB-003", @"D:\PROB-004",
+                    @"D:\PROB-005", @"D:\PROB-006", @"D:\PROB-007", @"D:\PROB-008", @"D:\PROB-009",
+                    @"D:\PROB-010", @"D:\PROB-011", @"D:\PROB-012", @"D:\PROB-013", @"D:\PROB-014",
+                    @"D:\PROB-015", @"D:\PROB-004", @"D:\PROB-016", @"D:\PROB-017", @"D:\PROB-018",
+                    @"D:\PROB-019", @"D:\PROB-020", @"D:\PROB-021", @"D:\PROB-022", @"D:\PROB-023",
+                    @"D:\PROB-024", @"D:\PROB-026",@"D:\PROB-027",@"D:\PROB-028",@"D:\PROB-029",
+                    @"D:\PROB-030",@"D:\PROB-031",@"D:\PROB-032",@"D:\PROB-033",@"D:\PROB-034",
+                    @"D:\PROB-035",@"D:\PROB-036",@"D:\PROB-037",@"D:\PROB-038",@"D:\PROB-039",
+                @"D:\PROB-040",@"D:\PROB-041",@"D:\PROB-042",@"D:\PROB-043",@"D:\PROB-045",@"D:\PROB-046",
+                @"D:\PROB-047",@"D:\PROB-048",@"D:\PROB-049",@"D:\PROB-050",@"D:\PROB-051",@"D:\PROB-052",@"D:\PROB-053",
+                @"D:\PROB-054",@"D:\PROB-055",@"D:\PROB-056",@"D:\PROB-057",@"D:\PROB-058",@"D:\PROB-059",@"D:\PROB-060",
+                @"D:\PROB-061",@"D:\PROB-062",@"D:\PROB-063",@"D:\PROB-064",@"D:\PROB-65",@"D:\PROB-66",@"D:\PROB-067",
+                @"D:\PROB-068",@"D:\PROB-069",@"D:\PROB-070"};
                 for (int i = 0; i < path.Length; i++)
                 {
                     richTextBox1.AppendText(path[i] + "\n");
@@ -217,6 +222,19 @@ namespace FileWatcher
                             richTextBox1.AppendText(shpFile.FullName + "\n");
                         }
                     }
+                    List<FileInfo> lstFiles1 = getFile(path[i], ".njdf", lst);
+                    foreach (FileInfo shpFile in lstFiles1)
+                    {
+                        string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
+                        string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
+                        //不存在同名的文件则进行转换
+                        if (!File.Exists(StartPath + Filename + ".xls"))
+                        {
+                            Thread.Sleep(1000);
+                            exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + shpFile.FullName);
+                            richTextBox1.AppendText(shpFile.FullName + "\n");
+                        }
+                    }
                 }
             }
             catch (Exception ex)
@@ -225,6 +243,55 @@ namespace FileWatcher
             }
         }
 
+        private void RunTran1()
+        {
+            try
+            {
+                List<FileInfo> lst = new List<FileInfo>();
+                string[] path = textBox1.Text.Split(',');
+                for (int i = 0; i < path.Length; i++)
+                {
+                    richTextBox1.AppendText(path[i] + "\n");
+                    List<FileInfo> lstFiles = getFile(path[i], ".jdf", lst);
+                    foreach (FileInfo shpFile in lstFiles)
+                    {
+                        string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
+                        string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
+                        //不存在同名的文件则进行转换
+                        if ((!File.Exists(StartPath + Filename + ".xls") && (new FileInfo(shpFile.FullName).Length / 1024 > 50)) || (new FileInfo(StartPath + Filename + ".xls").Length / 1024) < 100)
+                        {
+                            Thread.Sleep(1500);
+                            exec(@"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe", @"D:\FileWatcher\JDF\DTS-JDFData2Excel.exe " + shpFile.FullName);
+                            richTextBox1.AppendText(shpFile.FullName + "\n");
+                        }
+                    }
+                    List<FileInfo> lstFiles1 = getFile(path[i], ".njdf", lst);
+                    foreach (FileInfo shpFile in lstFiles1)
+                    {
+                        string Filename = shpFile.FullName.Substring(shpFile.FullName.LastIndexOf(@"\") + 1).Split('.')[0];
+                        string StartPath = shpFile.FullName.Substring(0, shpFile.FullName.LastIndexOf(@"\") + 1);
+                        //不存在同名的文件则进行转换
+                        try
+                        {
+                            if (!File.Exists(StartPath + Filename + ".xls") || (new FileInfo(StartPath + Filename + ".xls").Length / 1024) < 2000)
+                            {
+                                Thread.Sleep(1500);
+                                exec(@"D:\FileWatcher\NJDF\DTS-Data2Excel.exe", @"D:\FileWatcher\NJDF\DTS-Data2Excel.exe " + shpFile.FullName);
+                                richTextBox1.AppendText(shpFile.FullName + "\n");
+                            }
+                        }
+                        catch (Exception ex)
+                        {
+                            richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
+                        }
+                    }
+                }
+            }
+            catch (Exception ex)
+            {
+                richTextBox1.AppendText(ex.Message + ex.StackTrace + "\n");
+            }
+        }
 
         /// <summary>
         /// 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)
@@ -236,7 +303,6 @@ namespace FileWatcher
         {
             try
             {
-
                 string[] dir = Directory.GetDirectories(path); //文件夹列表  
                 DirectoryInfo fdir = new DirectoryInfo(path);
                 FileInfo[] file = fdir.GetFiles();
@@ -267,5 +333,11 @@ namespace FileWatcher
         {
             RunTran();
         }
+
+        private void button1_Click(object sender, EventArgs e)
+        {
+            Thread tr = new Thread(RunTran1);
+            tr.Start();
+        }
     }
 }