|
BATのマクロ的な使用例を紹介。手抜きですがそれっぽく動作してます。 |
|
・pwdコマンドのようなBAT
cdコマンドを実行してるだけ。 @ECHO OFF SETLOCAL SET THIS_BAT=pwd.bat IF "%1"=="/?" GOTO OUTPUT_HELP IF "%1"=="-h" GOTO OUTPUT_HELP IF "%1"=="--help" GOTO OUTPUT_HELP cd GOTO END :OUTPUT_HELP ECHO This command is "%THIS_BAT%". GOTO END :END SET THIS_BAT= ENDLOCAL GOTO :EOF |
|
・whichコマンドのようなBAT
PATHに設定されているフォルダ内にある、拡張子が「.exe」「.bat」「.com」のファイルを表示する。 @echo off setlocal set THIS_BAT=which.bat if "%1"=="" goto OUTPUT_HELP if "%1"=="/?" goto OUTPUT_HELP if "%1"=="--help" goto OUTPUT_HELP for %%V in (%1 %1.exe %1.bat %1.cmd) do ( if not "%%~$Path:V"=="" echo %%~$Path:V ) goto END :OUTPUT_HELP echo Usage: %THIS_BAT% Commmand-Name goto END :END endlocal goto :EOF |
|
・grepコマンドのようなBAT
findstr.exeを呼び出してるだけ。 @echo off setlocal set THIS_BAT=grep.bat set CMD=%windir%\system32\findstr.exe /R set WK_ARGS= if "%1"=="" goto OUTPUT_HELP :LOOP_OPEN set WK=%1 if "%WK%"=="" goto LOOP_BREAK if "%WK%"=="-i" set WK=/I if "%WK%"=="-n" set WK=/N if "%WK%"=="-in" set WK=/I /N if "%WK%"=="-ni" set WK=/N /I set WK_ARGS= %WK_ARGS% %WK% shift goto LOOP_OPEN :LOOP_BREAK echo -^> %CMD% %WK_ARGS% %CMD% %WK_ARGS% goto END :OUTPUT_HELP echo Usage: grep [-i] [-n] "String" FileName echo This command is "%THIS_BAT%" goto END :END set WK_ARGS= endlocal goto :EOF |
| 一つ上に | 次 | トップページへ | ググる | lowry1213@yahoo.co.jp |