2
4

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ファイル内でパイプを使いたい!

Posted at

TL;DR

ネット記事に正しい情報があんま載ってないっぽい、
バッチファイル内でパイプする方法のご紹介
実はあるあるネタだったらすいません、、

まえせつ

コマンド打って特定の文字をひっかけるような脳死テストを
バッチファイルで書こうとすると

バッチ スクリプト外でバッチ ラベルを呼び出すことはできません。

にブチあたったりしません?
バッチファイル内でパイプすると起きます。
コマンドプロンプトから成功してもパイプ使うと発生します。

Using pipe command in a batch file

こういう記事とかに ^ (キャレット) 使って!と書いてあるけど
どうにも解消されません。。

色試して解決しました。

とりせつ

なんと以下のように " を二重に囲う事で解決します!

@echo off

call :checkFunction "reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\DataCollection" | find "REG_DWORD" | find "0x1""
REM call :checkFunction "(テストコマンドをがんがん書いてく)"
exit /b

:checkFunction
cmd /c %*  > nul 2>&1
if %errorlevel% neq 0 (
	echo %*が失敗した!
	exit /b 0
)
echo %*が成功した!
exit /b 1

おしまい

これで終了コードでは判定出来ないようなレジストリの値チェックや、
さらにpowershell呼び出すようなケースでも。
追加ツール導入しないでチェックできますね!やったね!!

追加でツール入れられない環境なんて無いって??あるんですよ!!

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?