batファイルで「echo」の使い方を今回は行います。
「echo」コマンドは指定の文字列を標準出力に表示する事が出来るコマンドです。
ただ文字列を表示するだけでなく、ファイルを作成したりする事も可能です。

中々奥が深いので、わかりやすいように実例を交えながらやっていきましょう!
windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。
batファイルで「echo」コマンドについて


batファイルでの「echo」コマンド基礎
batファイルで「echo」コマンドの基礎は以下です。
echo 指定の文字列
これで指定した文字列を表示する事が可能です。




batファイルで「echo」でエラーが出た場合の対処法


では実際にbatファイルを実行してみます!
@echo off
echo 文字を表示します。
上のbatファイルを実行してみましょう!




エラーで文字化けしてしまっています。
batファイルで文字化けエラーの解消法
ファイルを見てみると文字コードが「UTF-8」になっています。
「ファイル」→「名前を付けて保存」で文字コードを「ANSI」にして上書き保存します。


もう一度同じbatファイルを実行してみます。


エラーが解消され「文字を表示します」が表示されました。
batファイルで「echo」でファイルを作成する方法


batファイルでechoは文字を表示するだけのコマンドではありません。
ファイルを作成する事が可能です。
@echo off
echo 文字を記入します > echo.txt
上のbatファイルを実行して見ます。


実行したbatファイルと同じフォルダに「echo.txt」が作成されています。
中身は「文字を記入します」という文字列が入っています。
batでファイルを作るその他の方法は、以下の記事で纏めていますので良かったらご覧ください。


batファイルの「echo」で空白を表示する
今度はbatの「echo」で空白を表示してみます。
@echo off
echo 文字を表示します。
echo.
echo 文字を表示します。
上のbatファイルを実行してみます!
実行結果は以下になりました。空白ができています。
C:\script\bat_echo>echo.bat
文字を表示します。
文字を表示します。
C:\script\bat_echo>
空白を使用して文字を綺麗にするにはオススメです。
batファイルの「@echo off」の意味とは
batファイルの1行目にある「@echo off」ですが、今までのechoとは少し意味が違います。
batファイルでは記述したコマンドも表示されるのですが、「@echo off」を記述しておけば表示されません。
実行結果だけが表示されて見えやすいというメリットがあります。


まとめ:batファイルの「echo」の使い方


batファイルの「echo」の使い方の基礎は以下のコードです。
echo 指定の文字列
指定の文字が文字化けする場合は文字コードを「ANSI」にする。
batでechoのその他の使い方として、ファイルを作成する方法があり、やり方は以下のコードでした。
@echo off
echo 文字を記入します > echo.txt
他にも空白を表示するやり方もありました。そのコードは「echo.」で例文は以下になります。
@echo off
echo 文字を表示します。
echo.
echo 文字を表示します。
batファイルの「echo」をどんどん使用して見て下さい。