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?

More than 3 years have passed since last update.

BATのgotoでコマンド構文が誤っている場合

Posted at

NGなBATファイル

tset.bat
@echo off
set val=123
set num=456
if %val%==123 (
	if %num%==456 (
		goto hoge
	)
	echo sakura
	:hoge

	echo warabi
)

これを実行した結果は以下

C:\Users\Administrator\Desktop>test.bat
コマンドの構文が誤っています。

OKなBATファイル

test.bat
@echo off
set val=123
set num=456
if %val%==123 (
	if %num%==456 (
		goto hoge
	)
	echo sakura
	:hoge
	echo warabi
)

これを実行した結果は以下

C:\Users\Administrator\Desktop>test.bat
warabi

おわかりいただけただろうか。

原因&対処

if()のなかのgotoラベルは直後にコマンドが無いと駄目らしい。
1つ目と2つ目の違いは:hogeの直後に空行があるかないか、だけである。

コマンドならなんでもよくて、コメント文であるremでもいい。

test.bat
@echo off
set val=123
set num=456
if %val%==123 (
	if %num%==456 (
		goto hoge
	)
	echo sakura
	:hoge
	rem #本日は晴天なり
	echo warabi
)

goto使ってコマンドの構文が誤っていますと言われた場合はチェックしてみてください。

あとif()の中でなければ問題はない

test.bat
@echo off

goto hoge

:hoge

echo warabi
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?