batでcallの使い方について教えます。

似たコマンドの「goto」や「start」がありますが、どのように使い分けたら良いのですか?
そう言った疑問にも現役エンジニアの筆者が答えていきます。
windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。
batでcallの使い方を学んで、「goto」や「start」などの使い分けが出来れば、自由自在なbatファイルを設計する事ができます。
batでのcallの基本の使い方


batでのcallの基本の使い方としては以下の2点になります。
- ラベルに飛ぶ
- 外部ファイルを呼び出す
callでラベルまで飛ぶ
callでラベルまで飛ぶ事が可能です。
ラベルを作成してそこまで飛びます。そこから最後まで行くと「call」の後からまた処理が始まります。
callで外部ファイルを呼び出す
callで外部ファイルを呼び出す事が可能です。
外部のファイルを表示したり実行することもできます。
外部テキストファイルを開いたとして、閉じると「call」の後からまた処理が始まります。
他のbatファイルを開いたとしたら、そのbatファイルの処理が終われば「call」の後からまた処理が始まります。
実践でcallを使用してみる


callでラベルまで飛ぶか実践する
では実際にbatファイルでcallコマンドを試してみます。まずはラベルまで飛ぶ動きの確認です。
- ラベルをつけてそこに飛ぶ事を確認
- batの最後まで行く事を確認
- callの後から処理が再開される事を確認
下のbatファイルを記述して実行してみます。
@echo off
echo ①この行が最初の行です!
REM lastのラベルまで飛びます
call :last
echo まずこの行が表示されなければ成功
:last
echo ②この行が最後の行です!
pause
callでラベルまで飛ぶbatの説明




ラベルをつけるには「:」の後に自分の好きな名前をつけます。ここでは「:last」というラベルをつけています。
「call :last」コマンドでラベルまで飛ぶ事ができます。


では実際に実行してみます。


callでラベルまで飛ぶbatの実行結果説明
①この行が最初の行です!
②この行が最後の行です!
まずこの行が表示されなければ成功
②この行が最後の行です!
続行するには何かキーを押してください . . .
まず初めのechoの文字列が表示されます。
ラベルまで飛んだので2つ目のechoの文字列は表示されず、最後の文字列が表示されています。
batの最後まで行ったのでcallの後から処理が開始され、2つ目の文字列が表示されます。
また最後の文字列が表示されて、batが終わっています。
違和感のある動きですが、想定通りの動きになりました。
callで外部ファイルを呼び出してみる
次にcallで外部ファイルを呼び出してみましょう。
まずは下のbatファイルを作成します。
@echo off
echo ①この行が最初の行です!
REM 外部ファイルを呼び出します
call another_file.txt
call another_file.bat
echo ②この行が最後の行です!
pause
このbatファイルと同じ階層に「anather_file.txt」と「anather_file.bat」を作成します。内容は以下です。
this is another text file!
@echo off
echo this is another bat file!
callで外部ファイルを呼び出すbatの説明
まずは最初の文字を表示します。
その後にcallで外部のテキストファイルを呼び出しています。
その後にまたcallで外部batファイルを呼び出しています。
そして最後の文字列を表示して終了です。



ではこのbatを実行してみます。
callで外部ファイルを呼び出すbatの実行結果説明
①この行が最初の行です!
まずは最初の文字列が表示されて、外部テキストファイルが開きました。
開いたテキストを閉じます。callで外部テキストファイルを呼び出した後から処理が継続して、次の外部batファイルのechoコマンドが実行されて、このプロンプト上に文字列が表示されています。
this is another bat file!
外部batファイルが終わりまで行ったので、呼び出したcallの後から処理が継続して最後の文字列が表示されました。
②この行が最後の行です!
続行するには何かキーを押してください . . .
想定通りの動きになりました。
callとgotoの違いについて





batファイルでラベルに移動するコマンドで「goto」があるけど「call」とどう違うの?
callとgotoの違いについての説明
ではcallとgotoの違いは何なのでしょうか?
先程の「call」はbatの終わりまで行くとcallの下からまた処理が始まりますが、「goto」はそのまま終わるところが違いです。
先程のbatファイルの「call」を「goto」に変えて実行してみます。
@echo off
echo ①この行が最初の行です!
REM lastのラベルまで飛びます
goto :last
echo まずこの行が表示されなければ成功
:last
echo ②この行が最後の行です!
pause
callをgotoに修正して実行した結果
callをgotoに修正して実行した結果です。
①この行が最初の行です!
②この行が最後の行です!
続行するには何かキーを押してください . . .
初めの文字列が表示されたあと、ラベルに飛び最後の文字列が表示されて終了しています。
「call」と「goto」の違いは飛んだあと戻ってくるか来ないかです。



1つのbatの中であれば「call」よりも「goto」の方が使いやすいですね。
callとstartの違いについて





batで外部ファイルを呼び出すコマンドで「start」があるけど「call」とどう違うの?
callとstartの違いについての説明
ではcallとstartの違いは何なのでしょうか?
- 外部ファイルの実行終了を待たない
- 外部batファイルは別ウィンドウで開く
先程の「call」は外部ファイルが閉じられたり、外部batが終了するまで待ち、そのあとcallの下から処理が再開しますが、「start」は外部の終了を待たない事が違いです。
また外部batファイルは別ウィンドウを開いて実行します。
先程のbatファイルの「call」を「start」に変えて実行してみます。
@echo off
echo ①この行が最初の行です!
REM 外部ファイルを呼び出します
start another_file.txt
start another_file.bat
echo ②この行が最後の行です!
pause
callをstartに修正して実行した結果
callをstartに修正して実行した結果です。
①この行が最初の行です!
②この行が最後の行です!
続行するには何かキーを押してください . . .
外部テキストファイルが開かれて、外部batファイルも別ウィンドウで開かれています。
初めの文字列が表示されたあと、外部ファイルが閉じるのを待たずして、一気に最後の文字列が表示されて終了しています。
「call」と「start」の違いは外部ファイルの処理を待つか待たないか。また外部batを別ウィンドウで表示するかしないかになります。



外部ファイルの終了を待つcallが使いやすいです。
総括:batでcallの使い方!gotoやstartとの違いについて


batでのcallの基本の使い方としては以下の2点になります。
- ラベルに飛ぶ
- 外部ファイルを呼び出す
gotoとの違いはラベルに飛んだ後callの下から処理が再開する。
callはラベルに飛ぶなどそのbatで完結する場合にはふさわしくない。「goto」コマンドを使用しましょう!


callで外部ファイルを呼び出すときはstartと違い、外部ファイルが閉じたり処理が終了するのを待つので扱いやすい
外部ファイルを呼び出すときは「call」を使用しましょう!