0

我想从我们称为“List.txt”的外部列表(简单的一行中的每个单词)中搜索单词列表,然后在文件中搜索它们(C:\Users\P Ditty\ Documents\SH3\data\cfg\Backups_SCR*.clg) (匹配整个单词,即使它在另一个单词中,并且匹配大小写),然后如果它们在那里,则在另一个文件中找到这些单词(Campaign_SCR.mis.tmp)(匹配整个单词,即使它在另一个匹配的大小写中)仅当行以“Name =”开头时,才将 (Campaign_SCR.mis.tmp) 中的整行替换为 Name=ShipDummy。之后,同一文件中下面的两行将替换为第二行“Class=ShipDummy”,然后是第三行“Type=206”。

这是我现在拥有的代码。现在它在 C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg 中搜索 ClassName= Variable,然后如果在 Campaign_SCR.mis.tmp 中找到该变量,它将用 Class=ShipDummy 替换整行. 它还将其下方的 1 行替换为“Type=206”。

setlocal enableDelayedExpansion

>Campaign_SCR.mis.tmp (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" Campaign_SCR.mis.backup') do (
    ( echo !ln!| findstr "^Type=12$" >NUL && set ln=ln ) || (
        set "ln=%%B"
        if "!ln:~0,6!"=="Class=" (
            findstr /s /c:"ClassName=!ln:~6!" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\*.clg" >"C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR\null" && (
                echo Class=ShipDummy
                set "ln=Type=12"
            )
        )
        if #!ln!==# (echo;) else echo !ln!
    )
  )
)

但我希望它从我指定的列表中搜索名称,在 (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) 中搜索。然后,如果找到匹配项,则在 Campaign_SCR.mis.tmp 中搜索并替换整行,前提是该行以“Name=”开头,Name=ShipDummy。下面的两行将替换为第二行“Class=ShipDummy”,然后是第三行“Type=206”:

Name=ShipDummy
Class=ShipDummy
Type=206

**请记住,在 Campaign_SCR.mis.tmp 中,变量的行通常不会显示为 Name=variable。很可能是 Name=(words)variable(words)

被搜索的列表可以是外部的吗?我们可以让它每行搜索一个单词吗?例如:如果列表如下:

俾斯麦引擎盖击退

它将搜索每个单词。**

当它匹配列表中的单词到 (C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) 时,请确保它也匹配大小写。所以列表中的“Bismarck”将从(C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg)中找到“Bismarck”,而不是找到“bismarck”

感谢您的时间!

顺便说一句,对于我在这里遇到的问题,这可能是一个更简单的解决方法:如何应用它?

例如:

假设我有一个看起来像这样的外部列表(例如称为 List.txt):

Bismarck
Hood
Repulse

我将为此搜索 C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg。假设它在以下位置找到“俾斯麦”和“胡德”:

sadfasfasfdBismarckfasdfasdfasdfas

asdfasfdafHoodasdfasfas

然后它将在 Campaign_SCR.mis.tmp 中搜索俾斯麦和胡德替换:

Name=asdfBismarckasfdw
Class=jlkjf
Type=12

和:

Name=ShipDummy
Class=ShipDummy
Type=206

Name=asdfHoodasfdw
Class=jlkjf
Type=13

和:

Name=ShipDummy
Class=ShipDummy
Type=206
4

1 回答 1

1
@echo off
setlocal EnableDelayedExpansion

REM INITIALIZE THE LIST OF WORDS THAT WILL BE SEARCHED
set targetWords=:EOF

rem I'd like to search for a list of words from an external list (simple each word on a line)
for /F %%a in (List.txt) do (   
   rem and search for them in a file (C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg) 
   findstr "%%a" "C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups_SCR*.clg" > NUL
   rem if they are there...
   if !errorlevel! equ 0 (
      REM INSERT THE WORD IN THE TARGET LIST
      set targetWords=!targetWords! %%a
   )
)

REM INSERT THE END-OF-FILE MARK IN THE FILE
echo :EOF>> Campaign_SCR.mis.tmp

REM INITIALIZE THE NUMBER OF LAST PROCESSED LINE IN REDIRECTED Campaign_SCR.mis.tmp
set lastLine=0

rem ... find those words on another file(Campaign_SCR.mis.tmp)
< Campaign_SCR.mis.tmp (for /F "delims=:" %%a in ('findstr /N "%targetWords%" Campaign_SCR.mis.tmp') do (
   REM DUPLICATE PREVIOUS LINES UNTIL NEW TARGET LINE
   set /A numOfLines=%%a-lastLine-1
   for /L %%i in (1,1,!numOfLines!) do (
      set line=
      set /P line=
      echo(!line!
   )
   rem if the line starts with "Name="
   set /P line=
   if "!line:~0,5!" equ "Name=" (
      rem replacing the whole line...with Name=ShipDummy
      echo Name=ShipDummy
      rem After that the two lines below that in that same file would be replaced with 2nd line "Class=ShipDummy", 
      set /P line=
      echo Class=ShipDummy
      rem then 3rd line "Type=206".
      set /P line=
      echo Type=206
      set /A lastLine=%%i+2
   ) else (
      REM DUPLICATE THE NON MATCHING LINE, IF IS NOT THE :EOF MARK
      if "!line!" neq ":EOF" (
         echo !line!
         set lastLine=%%i
      )
   )
)) > Campaign_SCR.mis.tmp.NEW

REM UPDATE THE NEW FILE
REM del Campaign_SCR.mis.tmp
REM ren Campaign_SCR.mis.tmp.NEW Campaign_SCR.mis.tmp
于 2013-02-23T00:47:33.577 回答