0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

極copy.BAT を目指す。志は高く持て。バッチファイル入門

Posted at

単なるコピーの練習用バッチの作り方
https://qiita.com/tattyan39/items/72cd63e9edfdc0f62459
の続き。

Shift_JIS pleese.

皆様はもう少し上手に出来ると思う。

auto_copy1.bat
@echo off
setlocal enabledelayedexpansion
rem [command(cp) A B (-s[-sabu])] or [[copy]["A"] ["B"] (-s)]
rem This fail encode Shift_JIS
if "%~1"=="stato_copy" (
    set "userInput=copy"

    :: 第2引数(コピー元)
    set "arg2=%~2"
    set "first2=%arg2:~0,1%"
    if "%first2%"=="\"" (
        set "userInput=!userInput! %arg2%"
    ) else (
        set "userInput=!userInput! \"%arg2%\""
    )

    :: 第3引数(コピー先)
    set "arg3=%~3"
    set "first3=%arg3:~0,1%"
    if "%first3%"=="\"" (
        set "userInput=!userInput! %arg3%"
    ) else (
        set "userInput=!userInput! \"%arg3%\""
    )

    :: 第4引数(オプション)
    if "%~4"=="" (
        goto loop
    ) else (
        set "userInput=!userInput! %~4"
    )

    goto syoutoin
)
:loop
echo [形式: cp/copy] [引数: A B] [オプション: -s sabu] or Ctrl+cキャンセル
set /p userInput=何か入力してください(Ctrl+Cでキャンセル):
:syoutoin 
if "!userInput!"=="" goto loop

echo 入力: !userInput!

set i=0
set "quflag=0"
set "quotw="

for %%A in (!userInput!) do (
    set "raw=%%A"
    call set "firstChar=%%raw:~0,1%%"
    call set "lastChar=%%raw:~-1%%"

    :: 引用符の開きと閉じを除去
    if "!firstChar!"=="\"" (
        call set "raw=%%raw:~1%%"
    )
    if "!lastChar!"=="\"" (
        call set "raw=%%raw:~0,-1%%"
    )

    :: ダブルクォートを全て除去
    set "tokens=!raw:"=!"

    if "!quflag!"=="0" (
        if "!firstChar!"=="\"" (
            set "quflag=1"
        )
        if "!lastChar!" NEQ "\"" (
            set "quotw=!quotw! !tokens!"
        ) else (
            set "quotw=!quotw! !tokens!"
            set /a i+=1
            set "arg!i!=!quotw: =%%20!"
            set "quotw="
            set "quflag=0"
        )
    ) else (
        set /a i+=1
        set "arg!i!=!tokens!"
    )
)

:: 表示確認
for /l %%I in (1,1,!i!) do (
    echo 第%%I引数: !arg%%I!
)

if "!arg1!"=="cp" set "arg1=copy"
if "!arg1!"=="copy" (
    echo copy コマンドが選ばれました。
) else (
    echo 未知のコマンド pleese [cp]or[copy]: !arg1!
    goto loop
)
echo 第1引数: !arg1!

:: ディレクトリが存在するか確認
if "!arg2!"=="" (
	echo it A="?" to input.
	goto loop
)
if exist "!arg2!\" (
    echo "!arg2!" の中にあるファイル一覧:

    for %%F in ("!arg2!\*") do (
        echo - %%~nxF
    )
) else (
    echo ディレクトリ "!arg2!" は存在しません。
)
echo 第2引数: !arg2!
:: ディレクトリが存在するか確認
if exist "!arg3!\" (
    echo "!arg3!" の中にあるファイル一覧:

    for %%F in ("!arg3!\*") do (
        echo - %%~nxF
    )
) else (
    echo ディレクトリ "!arg3!" は存在しません。
choice /c ync /n /m "続けますか? [y/n/c]"
if errorlevel 3 (
    echo c が選ばれました。
    pause
    exit
) else if errorlevel 2 (
    echo n が選ばれました。
    goto loop
) else if errorlevel 1 (
    echo y が選ばれました。
	if not exist "!arg3!" mkdir "!arg3!"
)	
)
echo 第3引数: !arg3!

if "!arg4!"=="-sp" (
    set arg4=supar
	goto in_arg4_ok
)
if "!arg4!"=="" (
    set arg4=nonin
	goto in_arg4_ok
)
if "!arg4!"=="-s" (
    set arg4=sabu
	goto in_arg4_ok
)
if "!arg4!"=="-sabu" (
	set arg4=sabu
    echo sabu コマンドが選ばれました。
) else (
    echo 未知のコマンド pleese not sabu [-s]or[-sabu]: !arg4!
    goto loop
)
:in_arg4_ok
echo 第4引数オプション: !arg4!

choice /c ync /n /m "続けますか? [y/n/c]"
if errorlevel 3 (
    echo c が選ばれました。
    pause
    exit
) else if errorlevel 2 (
    echo n が選ばれました。
    goto loop
) else if errorlevel 1 (
    echo y が選ばれました。
)
::                      ret copy
:: コピー元ディレクトリ(第2引数)
set "sourceDir=!arg2!"

:: コピー先ディレクトリ
set "targetDir=!arg3!"
set "imageDir=%targetDir%\image"

:: 作成
if not exist "%targetDir%" mkdir "%targetDir%"
if not exist "%imageDir%" mkdir "%imageDir%"

:: 拡張子リスト(画像ファイル)
set "imageExt=.jpg .jpeg .png .gif .bmp .webp"
if "!arg4!"=="sabu" goto subedir
if "!arg4!"=="supar" goto sel4_super
:: ファイル処理
for %%F in ("%sourceDir%\*") do (
    set "file=%%~nxF"
    set "ext=%%~xF"

    set "isImage=0"
    for %%E in (%imageExt%) do (
        if /i "%%E"=="!ext!" set "isImage=1"
    )

    if "!isImage!"=="1" (
        echo 画像: !file! → image にコピー
        copy "%%F" "%imageDir%\!file!" >nul
    ) else (
        echo その他: !file! → copy_all にコピー
        copy "%%F" "%targetDir%\!file!" >nul
    )
)

echo コピー完了。
goto end_to

:subedir
:: サブディレクトリも含めて巡回
for /r "%sourceDir%" %%F in (*) do (
    set "file=%%~nxF"
    set "ext=%%~xF"
    set "isImage=0"

    for %%E in (%imageExt%) do (
        if /i "%%E"=="!ext!" set "isImage=1"
    )

    if "!isImage!"=="1" (
        echo [画像] !file! → image にコピー
        copy "%%F" "%imageDir%\!file!" >nul
    ) else (
        echo [その他] !file! → 整理済 にコピー
        copy "%%F" "%targetDir%\!file!" >nul
    )
)

echo サブディレクトリも含めた整理完了。
goto end_to

:sel4_super
::setlocal enabledelayedexpansion
set /a i_img=0
set /a i_txt=0
set /a i_etc=0
set /a i_wwd=0
set "textDir=%targetDir%\text"
set "bakDir=%targetDir%\bak"
set "shiteiExt=lib user userID"

:: 拡張子リスト(画像)
:: set "imageExt=.jpg .jpeg .png .gif .bmp .webp"
set "textExt=.txt .text .dat .data .bat .sp1 .log"
:: サブディレクトリも含めて巡回
for /r "%sourceDir%" %%F in (*) do (
    set "file=%%~nxF"
    set "ext=%%~xF"
    set "isImage=0"
	set "isText=0"
	set "src=%sourceDir%\!file!"
	set "dst=%targetDir%\!file!"  :: デフォルトはその他
	set "tmpruto=%targetDir%"
	set "loop_endr=0"
	:: 現在のフォルダー名を取得
	for %%D in ("%CD%") do set "currentFolder="%%~nxD""
	set "currentFolder=!currentFolder: =%%20!"

	for %%E in (%imageExt%) do (
		if /I "%currentFolder%"=="lib" (
		set "tmpruto=%targetDir%\lib"
			if not exist "!tmpruto!\!currentFolder!" mkdir "!tmpruto!\!currentFolder!"
			for /r "%sourceDir%" %%F in (*) do (
				for %%D in ("%CD%") do (set "currentFolder=%%~nxD")
				set "currentFolder=!currentFolder: =%%20!"
				set "tmpruto=!tmpruto!\!currentFolder!"
				if not exist "%tmpruto%" mkdir "%tmpruto%"
				copy "%%F" "%tmpruto%\%%~nxF"
			)
			set "loop_endr=1"
		)
		if "%loop_endr%"=="0" (
			if /I "!currentFolder!"=="%%E" (
				if not exist "!tmpruto!\!currentFolder!" mkdir "!tmpruto!\!currentFolder!"
				echo このフォルダーでは処理をスキップします。
				set "loop_endr=1"
			)
		)
	)
	if "!loop_endr!"=="0" (
		for %%E in (%imageExt%) do (
			if /i "%%E"=="!ext!" (
				set "isImage=1"
				set "dst=%imageDir%\!file!"
			)
		)
		for %%E in (%textExt%) do (
			if /i "%%E"=="!ext!" (
				set "isText=1"
				set "dst=%textDir%\!file!"
			)
		)
		if exist "!dst!" (
			set /a i_wwd+=1
			echo [重複] !file! は既に image に存在します。\\bak\\ copy %targetDir%
			if not exist "%bakDir%" mkdir "%bakDir%"
			echo [@e] 画像: !file! → error kamo にコピー
			copy "%%F" "%bakDir%\!file!" >nul
		) else (
			echo [新規] !file! → 新規フォルダーにコピー
			if "!isImage!"=="1" (
				set /a i_img+=1
				if not exist "%imageDir%" mkdir "%imageDir%"
				echo [@a] 画像: !file! → image にコピー
				copy "%%F" "%imageDir%\!file!" >nul
			) else if "!isText!"=="1"(
				set /a i_txt+=1
				if not exist "%textDir%" mkdir "%textDir%"
				echo [@b] text: !file! → text にコピー
				copy "%%F" "%textDir%\!file!" >nul
			)else(
				set /a i_etc+=1
				echo [@c] その他: !file! → 整理済 にコピー
				copy "%%F" "%targetDir%\!file!" >nul
			)
		)
	)
)
echo --- 処理完了 ---
echo 絵巻に納めた数(画像): !i_img!
echo 書物に納めた数(txt) : !i_txt!
echo その他の記録者数: !i_etc!
echo 重なりファイル  : !i_wwd!
set /a total=!i_img!+!i_txt!+!i_etc!
echo 総巡礼者数(全ファイル): !total! 予備(不明) !i_wwd!
:: to end
:: @a,@b,@c,@e it ?
:end_to
::
exit /b 0

出展協力 coplot
考えたのは俺だが、命令語や使い方はアドバイスを受けました。
元々の作り方を無いところから、コピロットは作らないと思います。
論理的な追い込みや、不足分の補充は作者がやらないといけないと思っています。
coplorの協力は、大変有りがたく思いますが、経験上素直に動きません。
だから、これもうごかしていません。 ただ、ファイルが消えるのではなく、増えるのですから、一息の安心はあります。
皆様も、消えるのではないのに、技術研磨が出来ればと、出しました。
あなたが、やってみるか、バックアップは増えずに済むのかですね。
ですから、未完の物に、細かく言わない、変えたいところや、間違っているところは、皆様で見つけ出してほしい。極みなんて、本当はないんだから。

健闘を祈る。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?