2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【コマンドプロンプト】コマンドの結果を変数に格納し、表示する

2
Posted at

はじめに

Windowsのコマンドプロンプトを使用する際、コマンドの実行結果を変数に格納し、表示する方法を調べて、実装したので、方法を記事にしました

コマンドの書き方

Linux、及びmac osやAIX等で使用されるシェルスクリプトと混同しそうですが、以下のような構文です

for /f "usebackq" [変数、%%で指定する] in (`[コマンド]`) do set [ユーザーで指定する環境変数]=[変数]

使用例

cdコマンドを変数iに代入し、出力結果をユーザー指定の環境変数DIRECTORYに代入する場合

for /f "usebackq" %%i in (`cd`) do set DIRECTORY=%%i

実装したコマンドプロンプト

実装したコマンドプロンプトは以下になります。

@echo off
for /f "usebackq" %%i in (`cd`) do set DIRECTORY=%%i
echo %DIRECTORY%

実行結果

実行結果は以下の画像になります。
command_test.jpg

最後に

実務でコマンドプロンプトを使う例があまりないですが、忘備録として記事にしました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?