異なるサブディレクトリに同じ名前のファイルが多数あり、区切り文字に「}」が付いています。私は特定のテキストのセットを見つけて区切り文字の前にいくつかの新しいテキストを追加したい。たとえば:バッチまたはvbsを使用してファイル内のテキストを検索および置換/追加する
folder1\sample.css
.asd {}
.bar {}
folder2\sample.css
.foo {}
.bar {}
すべてにsample.txtファイルは、次のようになります。
..
.foo {display:none;}
..
は、どのように私はこの使用してVBS、PowerShellの、または好ましくはバッチファイルを実行しますか?これに似た 何か:
SETLOCAL ENABLEDELAYEDEXPANSION
for /r %%f in (*.txt) do (
for /f "delims=}" %%a in (%%f) do (
set str=%%a
REM modify the variable
REM replace the line with the variable
)
)
EDIT: 私はそれが動作しましたが、私は、これはより多くの再利用性とより良い書き込むことができたと確信しています。
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /r %%f in (index.css) do (
BatchSubstitute.bat ".foo {" ".foo { display:none;" %%f>%%f.new
del %%f
BatchSubstitute.bat ".bar {" ".bar { display:none;" %%f.new>%%f
del %%f.new
BatchSubstitute.bat ".asd {" ".asd { display:none;" %%f>%%f.new
del %%f
ren %%f.new index.css
)
BatchSubstitute.bat
@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
:$changed 20100115
:$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
CMDはこれには適していません。私はVBSとPowershellのどちらを使っても慣れ親しんでいることをお勧めします。 – Ben