windows 如何使用.bat文件将动态术语列表替换为计数?

aiqt4smr  于 2023-08-07  发布在  Windows
关注(0)|答案(1)|浏览(115)

这就是我现在要做的。我有一个A档和一个B档。
文件A将有一个术语列表,如:

  • 正文
  • 文本2

文件B中有很多代码。该代码将包含TEXT和TEXT2。
我需要分别用:1和:2替换TEXT和TEXT2。我可以让这个工作时,我硬编码的文本被取代,但我真的需要它的工作动态。
下面是将TEXT替换为的代码:1

SET count=1
for /F "tokens=*" %%A in (file_a.txt) do (
    call :subroutine %%A
)

:subroutine
    echo %count%:%1
    set "search=TEXT"
    set "replace=:%count%" 
    set /a count+=1

    set "textFile=file_b.txt"

    for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%textFile%" echo(!line:%search%=%replace%!
        endlocal
    )
 GOTO :eof

字符串
echo %count%:%1的行正确输出:1:TEXT.为什么我不能替换以下行:

set "search=TEXT"


有:

set "search=%1"


当我这样做的时候,输出文件只有:

line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:
line:=3:

sdnqo3pr

sdnqo3pr1#

我会这样做:

@echo off
setlocal

rem Load the list of replacement words from file_a
set count=0
for /F %%a in (file_a.txt) do (
   set /A "repl[%%a]=count+=1"
)

rem Do the replacements in file_b
for /F "delims=" %%i in (file_b.txt) do (
   set "line=%%i"
   setlocal EnableDelayedExpansion
   for /F "tokens=2,3 delims=[]=" %%x in ('set repl[') do (
      set "line=!line:%%x=:%%y!"
   )
   echo(!line!
   endlocal
)

字符串
样品输入:

Currently this is what I am trying to do. I have a file A and a file B.

File A will have a list of terms like:

TEXT
TEXT2

File B will have lots of code in it. This code will contain TEXT and TEXT2.

I need to replace TEXT and TEXT2 with :1 and :2 respectively. I can get this to work when I hard code in the TEXT to be replaced but I really need it to work dynamically.

Below is the code that will work to replace TEXT with :1


输出量:

Currently this is what I am trying to do. I have a file A and a file B.
File A will have a list of terms like:
:1
:2
File B will have lots of code in it. This code will contain :1 and :2.
I need to replace :1 and :2 with :1 and :2 respectively. I can get this to work when I hard code in the :1 to be replaced but I really need it to work dynamically.
Below is the code that will work to replace :1 with :1


你只需要完成一些小的调整。。

相关问题