call

バッチ プログラムを別のバッチ プログラムから呼び出します。


call

  • help call

Output:

バッチ プログラムを別のバッチ プログラムから呼び出します。

CALL [ドライブ:][パス]ファイル名 [バッチパラメーター]

  バッチパラメーター   バッチ プログラムで必要なコマンド ライン情報を指定します。

コマンド拡張機能を有効にすると、CALL は次のように変更されます:

CALL コマンドは、CALL のターゲットとしてラベルを受け付けるようになります。
構文は、次のとおりです:

    CALL :ラベル 引数

指定された引数で新しいバッチ ファイル コンテキストが作成され、指定
されたラベルの次の文に制御が渡されます。バッチ スクリプト ファイルの
最後に 2 回到達することによって、2 回 "終了" する必要があります。
1 回目に最後に到達したときには、制御は CALL 文の次の行に返されます。
2 回目に、バッチ スクリプトが終了します。バッチ スクリプトから "戻る"
ための GOTO :EOF 拡張機能の説明については、GOTO /? と入力してください。

また、バッチ スクリプトの引数参照 (%0%1 など) の展開は、次のように
変更されました:


    %* バッチ スクリプト内では、すべての引数 (%1%2%3%4        %5 など) を参照します。

    バッチ パラメーター (%n) の置換は拡張されました。次のオプション構文
    を使うことができます:

        %~1         - すべての引用句 (") を削除して、%1 を展開します。
        %~f1        - %1 を完全修飾パス名に展開します。
        %~d1        - %1 をドライブ文字だけに展開します。
        %~p1        - %1 をパスだけに展開します。
        %~n1        - %1 をファイル名だけに展開します。
        %~x1        - %1 をファイル拡張子だけに展開します。
        %~s1        - 展開されたパスは、短い名前だけを含みます。
        %~a1        - %1 をファイル属性に展開します。
        %~t1        - %1 をファイルの日付/時刻に展開します。
        %~z1        - %1 をファイルのサイズに展開します。
        %~$PATH:1   - PATH 環境変数に指定されているディレクトリを検索し、
                       最初に見つかった完全修飾名に %1 を展開します。
                       環境変数名が定義されていない場合、または
                       検索してもファイルが見つからなかった場合は、
                       この修飾子を指定すると空の文字列に展開されます。

    修飾子を組み合わせて、複合結果を得ることもできます:

        %~dp1       - %1 をドライブ文字とパスだけに展開します。
        %~nx1       - %1 をファイル名と拡張子だけに展開します。
        %~dp$PATH:1 - PATH 環境変数に指定されているディレクトリを
                       検索して %1 を探し、最初に見つかったファイル
                       のドライブ文字とパスだけに展開します。
        %~ftza1     - %1 を DIR の出力行のように展開します。

    上の例の %1 と PATH は、他の有効な値で置き換えることができ
    ます。%~ 構文は有効な引数の数によって区切られます。%~ 修飾子
%* と同時には使用できません。
Calls one batch program from another.

CALL [drive:][path]filename [batch-parameters]

  batch-parameters   Specifies any command-line information required by the
                     batch program.

If Command Extensions are enabled CALL changes as follows:

CALL command now accepts labels as the target of the CALL.  The syntax
is:

    CALL :label arguments

A new batch file context is created with the specified arguments and
control is passed to the statement after the label specified.  You must
"exit" twice by reaching the end of the batch script file twice.  The
first time you read the end, control will return to just after the CALL
statement.  The second time will exit the batch script.  Type GOTO /?
for a description of the GOTO :EOF extension that will allow you to
"return" from a batch script.

In addition, expansion of batch script argument references (%0, %1,
etc.) have been changed as follows:


    %* in a batch script refers to all the arguments (e.g. %1 %2 %3
        %4 %5 ...)

    Substitution of batch parameters (%n) has been enhanced.  You can
    now use the following optional syntax:

        %~1         - expands %1 removing any surrounding quotes (")
        %~f1        - expands %1 to a fully qualified path name
        %~d1        - expands %1 to a drive letter only
        %~p1        - expands %1 to a path only
        %~n1        - expands %1 to a file name only
        %~x1        - expands %1 to a file extension only
        %~s1        - expanded path contains short names only
        %~a1        - expands %1 to file attributes
        %~t1        - expands %1 to date/time of file
        %~z1        - expands %1 to size of file
        %~$PATH:1   - searches the directories listed in the PATH
                       environment variable and expands %1 to the fully
                       qualified name of the first one found.  If the
                       environment variable name is not defined or the
                       file is not found by the search, then this
                       modifier expands to the empty string

    The modifiers can be combined to get compound results:

        %~dp1       - expands %1 to a drive letter and path only
        %~nx1       - expands %1 to a file name and extension only
        %~dp$PATH:1 - searches the directories listed in the PATH
                       environment variable for %1 and expands to the
                       drive letter and path of the first one found.
        %~ftza1     - expands %1 to a DIR like output line

    In the above examples %1 and PATH can be replaced by other
    valid values.  The %~ syntax is terminated by a valid argument
    number.  The %~ modifiers may not be used with %*

Return Code: 1


example

_call1.bat

@echo call1
call _call2.bat

call :_test
@echo call1_1
:_test
@echo call1_2
goto:eof

@echo call1_3

_call2.bat

@echo call2
exit /b
@echo call2_1

呼び出し元に戻るには、goto :EOF または exit /B を使用します。

call1

c:\example>call _call2.bat
call2

c:\example>exit /b

c:\example>call :_test
call1_2

c:\example>goto:eof
call1_1
call1_2

c:\example>goto:eof

Expansion

_call3.bat

rem %%0 %0
copy C:\Windows\System32\find.exe find.exe
call :ParameterCheck "find.exe"
call :PATHCheck "find.exe"
set ORI=C:\Windows
call :ORICheck "explorer.exe"
@goto :EOF

:ParameterCheck
@echo      %%0 %0
@echo      %%1 %1
@echo     %%~1 %~1
@echo    %%~f1 %~f1
@echo    %%~d1 %~d1
@echo    %%~p1 %~p1
@echo    %%~n1 %~n1
@echo    %%~x1 %~x1
@echo    %%~s1 %~s1
@echo    %%~a1 %~a1
@echo    %%~t1 %~t1
@echo    %%~z1 %~z1
@echo   %%~dp1 %~dp1
@echo   %%~nx1 %~nx1
@echo %%~ftza1 %~ftza1
@goto :EOF

:PATHCheck
@echo        %%~f1 %~f1
@echo   %%~$PATH:1 %~$PATH:1
@echo %%~dp$PATH:1 %~dp$PATH:1
@goto :EOF

:ORICheck
@echo       %%~f1 %~f1
@echo   %%~$ORI:1 %~$ORI:1
@echo %%~dp$ORI:1 %~dp$ORI:1
@goto :EOF

オプション構文


c:\example>rem %0 .\_call3.bat

c:\example>copy C:\Windows\System32\find.exe find.exe
        1 個のファイルをコピーしました。

c:\example>call :ParameterCheck "find.exe"
     %0 :ParameterCheck
     %1 "find.exe"
    %~1 find.exe
   %~f1 c:\example\find.exe
   %~d1 c:
   %~p1 \example\
   %~n1 find
   %~x1 .exe
   %~s1 c:\example\find.exe
   %~a1 --a--------
   %~t1 2022/07/08 08:50
   %~z1 17920
  %~dp1 c:\example\
  %~nx1 find.exe
%~ftza1 --a-------- 2022/07/08 08:50 17920 c:\example\find.exe

c:\example>call :PATHCheck "find.exe"
       %~f1 c:\example\find.exe
  %~$PATH:1 C:\Windows\System32\find.exe
%~dp$PATH:1 C:\Windows\System32\

c:\example>set ORI=C:\Windows

c:\example>call :ORICheck "explorer.exe"
      %~f1 c:\example\explorer.exe
  %~$ORI:1 C:\Windows\explorer.exe
%~dp$ORI:1 C:\Windows\

Batch parameters

_call4.bat

call :test a,b,c
call :test "a b" c
call :test a "b,c",d
@goto:eof
:test
@echo %%1[%1] %%2[%2] %%3[%3]

引数はスペースかカンマで区切られます。cmd


c:\example>call :test a,b,c
%1[a] %2[b] %3[c]

c:\example>call :test "a b" c
%1["a b"] %2[c] %3[]

c:\example>call :test a "b,c",d
%1[a] %2["b,c"] %3[d]

最終更新 Sep 29, 2022 11:05 UTC
Built with Hugo
テーマ StackJimmy によって設計されています。