0

区切り文字が「}」の異なるサブディレクトリに、同じ名前のファイルが多数あります。特定のテキストのセットを見つけて、区切り文字の前に新しいテキストを追加したいと思います。例えば:

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

編集:私はそれを機能させました、しかし私はこれがより多くの再利用性ではるかに良く書かれることができると確信しています。

@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.
)
4

1 に答える 1

1

このバッチファイルを利用できます:http://www.dostips.com/?t = Batch.FindAndReplace

検索/置換を求めるファイルのループ内でそれを呼び出し.foo {}ます.foo{display:none;}

There are probably better find/replace binaries than using this find/replace batch, but it'll probably do the job.

于 2012-05-13T01:54:46.350 に答える