簡易コピーツール
リリース物件を格納しているフォルダから本番環境へコピーするのに、いちいちファイルを探してバックアップして…というのが果てしなく面倒でした。Installerつくればいいのだろうけれど、そんな手間もかけたくない。似たようなツールはきっとあるでしょうが、探すのも面倒だったので、えいやぁで作ってしまいました。バイナリ配布しようかと思ったけれど、release note書くの面倒だったので…
[仕様]
- コピー元とコピー先のフォルダ構成が同じになっている必要があります。
- バックアップフォルダを指定しなければ実行した日時でバックアップフォルダを作ります。
- 特殊なフォルダ(ハードリンクなど)は一切考慮していません。
[使い方]
コマンドラインから以下のようにして実行します。hoge.exeはここでの便宜的な名前です。プロジェクトで好きな名前を付けてください。
c:\>hoge.exe コピーもとフォルダ コピー先フォルダ バックアップフォルダ名(オプション)
コピー先フォルダに同名のファイルがあった場合、バックアップフォルダに移動してからコピーを行います。
[ソース]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using System; using System.Collections.Generic; using System.Text; using System.IO;
namespace makeReleaseFolder { /// <summary> /// c:\sourcefolderからd:\destfolderへ中身のコピーを行う処理。 /// ただし、d:\destfolderに同じ名前のコピー対象ファイル名が /// あれば、YYYYMMDDhhmmss(実行日時) /// という名前のフォルダを作成して、コピー対象ファイルをバックアップする。 /// コピー先に同名のファイルがない場合は、そのままコピーする。 /// </summary> class makeReleaseFolder { static void Main(string[] args) { string folderBackupName = null; if (args.Length < 2 || args.Length > 3) { Console.WriteLine( @"deployReleaseFolder [SourceFolder] [DestinationFolder] [Folder(optional)]"); return; } else { switch (args.Length) { case 2: DateTime dtNow = DateTime.Now; folderBackupName = dtNow.ToString("yyyyMMddHHmmss"); break; case 3: folderBackupName = args[2]; break; } }
bool Exist = Directory.Exists(args[1]); if (Exist == false) { Console.WriteLine(@"{0}が存在しません.", args[0]); return; } CopyFolder(args[0], args[1], folderBackupName); }
static int CopyFolder(string a_source, string a_destination, string a_BackupFolder) { if (Directory.Exists(a_destination) == false) { Directory.CreateDirectory(a_destination); }
string[] subDirectories = Directory.GetDirectories(a_source); foreach (string subDirFullpath in subDirectories) { string subDir = subDirFullpath.Substring(a_source.Length);
Directory.CreateDirectory(a_destination + subDir); bool Exists = Directory.Exists(a_source + subDir); if (Exists == true) { CopyFolder(a_source + subDir, a_destination + subDir, a_BackupFolder); } } PatchExec(a_source, a_destination, a_BackupFolder); return 0; }
static void PatchExec(string a_source, string a_destination, string a_backupfolder) { DirectoryInfo diSource = new DirectoryInfo(a_source); DirectoryInfo diDestination = new DirectoryInfo(a_destination); FileInfo[] filesPatchSource = diSource.GetFiles(); FileInfo[] filesPatchTarget = diDestination.GetFiles(); foreach (FileInfo fileSource in filesPatchSource) { foreach (FileInfo fileTarget in filesPatchTarget) { if (String.Compare(fileSource.Name, fileTarget.Name, true) == 0) { string folderBackup = a_destination + @"\" + a_backupfolder; bool Exists = Directory.Exists(folderBackup); if (Exists == false) { Directory.CreateDirectory(folderBackup); } string fileBackupName = folderBackup + @"\" + fileTarget.Name; File.Move(fileTarget.FullName, fileBackupName); Console.WriteLine("{0}を{1}に移動しました", fileTarget.FullName, fileBackupName); break; } } string fileDestFullName = a_destination + @"\" + fileSource.Name; File.Copy(fileSource.FullName, fileDestFullName); Console.WriteLine("{0}を{1}にコピーしました", fileSource.FullName, fileDestFullName); } } } } |
[使用条件など]
好きにしてください。特に著作権がどーとかいいません。いわゆるPDSとして扱ってもらってかまいません。
[独り言]
もうちょっとこうきれいにかけないかな…特にファイルコピー(PatchExec)のあたり。