LoginSignup
0
1

More than 5 years have passed since last update.

batファイルで、値を置換するとき

Last updated at Posted at 2019-04-02

きっかけ

メンバーみんなが使っているWindows環境で、ファイル内の文字列を置換するスクリプトがほしいなと。

置換のコマンドはこちらを使わせてもらうとして、あとは対象をバッチファイルに書いていくだけだが・・・
https://www.vector.co.jp/soft/win95/util/se271454.html

///にエスケープする必要があった。

わざわざエスケープ後の文字列を書いてくのはやだなーということで、batファイル内で値の置換ができないか調べた。

方法

@echo off

rem エスケープ前
set string=http://
echo 置換前:%string%

rem エスケープ実施
set replaced_string=%string:/=//%
echo 置換後:%replaced_string%

実行するとhttp://http:////となる。

この書き方だからかと思うが、置換対象の文字列は、setで一旦環境変数にする必要があった。

最後に

ここ最近、linuxだのpythonだのが多かったので、batファイルは中々新鮮すぎる。
ファイルのコピーにxcopyとかいうコマンドを使うのも初めて知った。

2019/4/17間違いの指摘受け、修正。
xpathxcopy

0
1
2

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
1