OSDN Git Service

初期インポート
[u6kcommons/rename-batch.git] / RenameBatch / Program.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.IO;\r
4 using System.Linq;\r
5 using System.Text;\r
6 \r
7 // $Id: Program.cs 357 2009-10-12 08:56:39Z u6k $\r
8 \r
9 namespace RenameBatch\r
10 {\r
11     class Program\r
12     {\r
13         static void Main(string[] args)\r
14         {\r
15             try\r
16             {\r
17                 /*\r
18                  * 引数チェック\r
19                  */\r
20                 if (args.Length != 1)\r
21                 {\r
22                     throw new ArgumentException("引数が1個ではありません。");\r
23                 }\r
24                 if (!Directory.Exists(args[0]))\r
25                 {\r
26                     throw new ArgumentException("引数[0]のフォルダ・パスが存在しません。");\r
27                 }\r
28                 if (!File.Exists(args[0] + "/before.txt"))\r
29                 {\r
30                     throw new ArgumentException("before.txtが存在しません。");\r
31                 }\r
32                 if (!File.Exists(args[0] + "/after.txt"))\r
33                 {\r
34                     throw new ArgumentException("after.txtが存在しません。");\r
35                 }\r
36 \r
37                 /*\r
38                  * before.txt, after.txtを読み込む。\r
39                  */\r
40                 string[] beforeArray = File.ReadAllLines(args[0] + "/before.txt", Encoding.Default);\r
41                 string[] afterArray = File.ReadAllLines(args[0] + "/after.txt", Encoding.Default);\r
42 \r
43                 if (beforeArray.Length != afterArray.Length)\r
44                 {\r
45                     throw new ArgumentException("before.txtとafter.txtの行数が異なります。");\r
46                 }\r
47 \r
48                 /*\r
49                  * リネームする。\r
50                  */\r
51                 for (int i = 0; i < beforeArray.Length; i++)\r
52                 {\r
53                     string beforePath = args[0] + "/" + beforeArray[i];\r
54                     string afterPath = args[0] + "/" + afterArray[i];\r
55 \r
56                     try\r
57                     {\r
58                         if (File.Exists(beforePath))\r
59                         {\r
60                             Console.WriteLine("[" + (i + 1) + "/" + beforeArray.Length + "]" + beforePath + " ...ファイル・リネーム成功");\r
61 \r
62                             File.Move(beforePath, afterPath);\r
63                         }\r
64                         else if (Directory.Exists(beforePath))\r
65                         {\r
66                             Console.WriteLine("[" + (i + 1) + "/" + beforeArray.Length + "]" + beforePath + " ...フォルダ・リネーム成功");\r
67 \r
68                             Directory.Move(beforePath, afterPath);\r
69                         }\r
70                         else\r
71                         {\r
72                             Console.WriteLine("[" + (i + 1) + "/" + beforeArray.Length + "]" + beforePath + " ...パス不存在");\r
73                         }\r
74                     }\r
75                     catch (IOException e)\r
76                     {\r
77                         Console.WriteLine("[" + (i + 1) + "/" + beforeArray.Length + "]" + beforePath + " ...エラー");\r
78                     }\r
79                 }\r
80             }\r
81             catch (Exception e)\r
82             {\r
83                 Console.WriteLine(e.ToString());\r
84 \r
85                 Console.WriteLine("usage: " + typeof(Program).Assembly.GetName().Name + ".exe [フォルダ・パス]");\r
86                 Console.WriteLine("    [フォルダ・パス]\\before.txt ... リネーム前のパス・リスト");\r
87                 Console.WriteLine("    [フォルダ・パス]\\after.txt  ... リネーム後のパス・リスト");\r
88             }\r
89 \r
90             Console.WriteLine("Push Any Key...");\r
91             Console.ReadLine();\r
92         }\r
93     }\r
94 }\r