如何在批处理文件中设置多行字符串替换

时间:2018-03-03 22:07:25

标签: batch-file batch-processing

我正在开发一个bat文件来运行我的实验,每个alpha1有两个不同的参数,有10个ws值,我使用bat文件更改实验代码中的变量,并使用10个子文件夹生成20个alpha文件每个alpha文件夹,我都看不出问题在哪里

@Echo off
SET "file=synch-ring.f"
SET /a Line#ToSearch1=14
SET /a Line#ToSearch2=27
FOR /L %%I IN (1,1,20) do (
FOR /L %%J IN (1,1,10) do (
md %%I
md %%I\%%J
(FOR /f "tokens=1*delims=:" %%a IN ('findstr /n "^" "%file%"') DO (
    SET "Line=%%a"
    IF %%a equ %Line#ToSearch% SET "Line=        Alpha1=%%I"
    SETLOCAL ENABLEDELAYEDEXPANSION
    ECHO(!Line! 
    ENDLOCAL
    IF %%a equ %Line#ToSearch2% SET "Line=        ws=%%J"
    SETLOCAL ENABLEDELAYEDEXPANSION
    ECHO(!Line!
    ENDLOCAL
    ))"%%J.f"
Move %%J.f %%I\%%J
))
pause

1 个答案:

答案 0 :(得分:2)

我不确定我是否已正确理解您的意图,但这可能对您有所帮助:

@Echo Off
Set "file=synch-ring.f"
Set "Line#ToSearch1=14"
Set "Line#ToSearch2=27"
If Not Exist "%file%" Exit /B
For /L %%I In (1,1,20) Do (
    For /L %%J In (1,1,10) Do (
        MD "%%I\%%J" 2>Nul
        (
            For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%file%"'
            ) Do (
                If %%A NEq %Line#ToSearch% (
                    If %%A NEq %Line#ToSearch2% (
                        Echo=%%B
                    ) Else (
                        Echo=        ws=%%J
                    )
                ) Else (
                    Echo=        Alpha1=%%I"
                )
            )
        )>"%%I\%%J\%%J.f"
    )
)
Pause