Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vbscript 使用批处理或vbs查找并替换/追加文件中的文本_Vbscript_Batch File_Cmd - Fatal编程技术网

Vbscript 使用批处理或vbs查找并替换/追加文件中的文本

Vbscript 使用批处理或vbs查找并替换/追加文件中的文本,vbscript,batch-file,cmd,Vbscript,Batch File,Cmd,我在不同的子目录下有许多同名文件,分隔符为“}”。我想找到一组特定的文本,并在分隔符之前添加一些新文本。例如: folder1\sample.css .asd {} .bar {} folder2\sample.css .foo {} .bar {} 所有sample.txt文件应如下所示: .. .foo {display:none;} .. 我如何使用vbs、powershell或批处理文件来实现这一点? 类似于此: SETLOCAL ENABLEDELA

我在不同的子目录下有许多同名文件,分隔符为“}”。我想找到一组特定的文本,并在分隔符之前添加一些新文本。例如:

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

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

您可以利用此批处理文件:

对文件循环中的文件进行调用,在文件循环中查找
.foo{}
并将其替换为
.foo{display:none;}


可能有比使用此查找/替换批处理更好的查找/替换二进制文件,但它可能会完成这项工作。

CMD与此不匹配。我建议您使用您更熟悉的VBS和Powershell。