LoginSignup
0
0

More than 3 years have passed since last update.

windowsのバッチのループ内で変数を置換する

Posted at
@echo off

setlocal enabledelayedexpansion

set before=hoge
set after=fuga

for /D %%f in (*.txt) do (
  set old=%%f
  set new=!old:%before%=%after%!

  echo !old!
  echo !new!
)

endlocal

pause

ポイント
・一文字変数のときは%を2つ先頭
・2文字以上変数のときは、%を前後に
・ループ内での変数を再利用するときは、遅延評価しないといけないので!で囲む
・set enabledelayedexpansion が無いと、!で遅延展開されない

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