/*
* CopyEx.dll の使い方を示すサンプルアプリケーションです。
* 実際にツールとしても使用しても問題ありません。
*
* Copyright(C) 2004 靖 東
*
* */
using System;
using ChinTools;
namespace Chin.Sample
{
///
/// CopyEx.dllの使い方を示すサンプルです。
///
class CpEx
{
///
/// アプリケーションのメイン エントリ ポイントです。
///
[STAThread]
static void Main(string[] args)
{
//バージョン表示
Console.WriteLine("CopyEx 1.0.1 Copyright(c) 2004 靖東");
Console.WriteLine("デモ cpEx 0.2");
if(args.Length == 0){
ShowUsage();
return;
}
if(args[0] == "/?"){
ShowUsage();
return;
}
string[] copySRC = null;
string[] copyDEST = null;
//デフォルトパラメータ
//コマンドラインより指定がなかればこれらが使用される
char ShowMessage = '1'; // エラー時メッセージ表示
char Overwrite = '0'; // 無条件上書きしない(確認画面が出る)
char SkipOverwrite = '0'; // 上書きをスキップしない(確認画面が出る)
char ContinueOnError = '0'; // エラー発生後は処理を続行しない
char Diff = '1'; // フォルダの差分上書き
bool MoveFile = false; // ファイルは移動しない
bool Log = true;
string LogPath = ""; // ログはコンソールに出力
bool WithAttribute = true; // 属性もコピー
bool BackGround = true; // 進捗画面表示しない
string searchPattern = "*"; // すべてのファイル
string temp;
//コマンドライン指定を設定
foreach(string s in args){
temp = s.ToLower();
if(temp.StartsWith("/s:") == true){
if(temp.Length == 3){
WrongParam(temp);
return;
}
else{
copySRC = temp.Substring(3).Split('/');
}
}
else if(temp.StartsWith("/d:") == true){
if(temp.Length == 3){
WrongParam(temp);
return;
}
else{
copyDEST = temp.Substring(3).Split('/');
}
}
else if(temp.StartsWith("/m:") == true){
if(temp.Length != 4){
WrongParam(temp);
return;
}else{
if(temp[3] == 'y'){
ShowMessage = '1';
}else if(temp[3] == 'n'){
ShowMessage = '0';
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/o:") == true){
if(temp.Length != 4){
WrongParam(temp);
return;
}else{
if(temp[3] == 'y'){
Overwrite = '1';
}else if(temp[3] == 'n'){
Overwrite = '0';
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/skip:") == true){
if(temp.Length != 7){
WrongParam(temp);
return;
}else{
if(temp[6] == 'y'){
SkipOverwrite = '1';
}else if(temp[6] == 'n'){
SkipOverwrite = '0';
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/c:") == true){
if(temp.Length != 4){
WrongParam(temp);
return;
}else{
if(temp[3] == 'y'){
ContinueOnError = '1';
}else if(temp[3] == 'n'){
ContinueOnError = '0';
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/i:") == true){
if(temp.Length != 4){
WrongParam(temp);
return;
}else{
if(temp[3] == 'y'){
Diff = '1';
}else if(temp[3] == 'n'){
Diff = '0';
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/delete:") == true){
if(temp.Length != 9){
WrongParam(temp);
return;
}else{
if(temp[8] == 'y'){
MoveFile = true;
}else if(temp[8] == 'n'){
MoveFile = false;
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/l:") == true){
if(temp.Length != 4){
WrongParam(temp);
return;
}else{
if(temp[3] == 'y'){
Log = true;
}else if(temp[3] == 'n'){
Log = false;
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/p:") == true){
if(temp.Length == 3){
WrongParam(temp);
return;
}else{
LogPath = temp.Substring(3);
}
}
else if(temp.StartsWith("/a:") == true){
if(temp.Length != 4){
WrongParam(temp);
return;
}else{
if(temp[3] == 'y'){
WithAttribute = true;
}else if(temp[3] == 'n'){
WithAttribute = false;
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/f:") == true){
if(temp.Length != 4){
WrongParam(temp);
return;
}else{
if(temp[3] == 'y'){
BackGround = false;
}else if(temp[3] == 'n'){
BackGround = true;
}
else{
WrongParam(temp);
return;
}
}
}
else if(temp.StartsWith("/search:") == true){
if(temp.Length == 8){
WrongParam(temp);
return;
}else{
if(temp[8] == 'f'){
searchPattern = "";
}
else{
searchPattern = temp.Substring(8);
}
}
}
else{
WrongParam(temp);
return;
}
}
//必須チェック
if(copySRC == null){
ShowUsage();
Console.WriteLine("\nエラー: コピー元(送り側)が必要です。");
return;
}
//必須チェック
if(copyDEST == null){
ShowUsage();
Console.WriteLine("\nエラー: コピー先(受け側)が必要です。");
return;
}
//ログ出力指定でかつコンソール出力でなければ
//LogPathはファイル名が必要 存在しない必要がある
if(Log == true && LogPath != ""){
if(System.IO.File.Exists(LogPath) == true){
Console.WriteLine("エラー: ファイルが既に存在しています。");
return;
}
else if(System.IO.Directory.Exists(LogPath) == true){
Console.WriteLine("エラー: ログの保存ファイル名が必要です。");
return;
}
try{
if( System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(LogPath)) == false ){
//存在しないファイルを受け取ってもOKだが、そのフォルダは存在しなければいけない
Console.WriteLine("エラー: ログの保存フォルダが存在しません。");
return;
}
}
catch{
Console.WriteLine("エラー: ログの保存フォルダが存在しません。");
return;
}
}
//コピー元(ファイルかフォルダ)の存在チェック
//この時点でチェックしても安全とは言えないが最低限のチェックは必要
for(int i=0; i
/// コマンドラインエラーを表示
///
/// s: 間違いのある部分のコマンドラインパラメータ
///
///
private static void WrongParam(string s){
ShowUsage();
Console.WriteLine("\nエラー: 認識できないパラメータが指定されました -> " + s);
}
///
/// アプリケーションの使い方を表示
///
private static void ShowUsage(){
Console.WriteLine("使用法:");
Console.WriteLine("/S: コピー元ファイル(またはフォルダ) 複数指定可 区切り(/)");
Console.WriteLine("/D: コピー先フォルダ 複数指定可 区切り(/)");
Console.WriteLine("/M:[Y/N] エラー発生時メッセージ表示する(Y)/しない(N)");
Console.WriteLine("/O:[Y/N] 上書き確認画面表示なしで無条件上書きする(Y)/しない(N)");
Console.WriteLine("/SKIP:[Y/N] 同じファイル名のファイルは上書きしないでスキップする(Y)/しない(N)");
Console.WriteLine("/C:[Y/N] エラー発生時でも処理を続ける(Y)/続けない(N)");
Console.WriteLine("/I:[Y/N] 上書きは差分(Y)/完全(N)");
Console.WriteLine("/DELETE:[Y/N] コピー後、コピー元のファイルを削除する(Y)/しない(N)");
Console.WriteLine("/L:[Y/N] ログを出力する(Y)/しない(N)");
Console.WriteLine("/P: ログファイルへのパス");
Console.WriteLine("/A:[Y/N] 属性もコピーする(Y)しない(N)");
Console.WriteLine("/F:[Y/N] 進捗画面表示する(Y)/しない(N)");
Console.WriteLine("/Search: コピーするファイルのパターン文字 すべてのファイル(*) フォルダのみ(f)");
Console.WriteLine("\n/S: /D: は指定必須です、他は指定されなければ、デフォルトが適当されます。");
}
}
}