我想搜索外部列表中的单词列表(一行中的每个单词简单),我们将其称为“List.txt”,并在文件中搜索它们(C:\ Users \ P Ditty \ Documents \ SH3 \ data \ cfg \ Backups_SCR * .clg)(匹配整个单词,即使它在另一个单词内,并匹配大小写),然后如果他们在那里找到那些单词文件(Campaign_SCR.mis.tmp)(匹配整个单词,即使它在另一个内部匹配,匹配大小写)只用行名称= ShipDummy取代整个行(Campaign_SCR.mis.tmp)用“Name =”。之后,同一文件中下面的两行将被第二行“Class = ShipDummy”替换,然后第三行“Type = 206”。
这是我现在的代码。现在它在C:\ Users \ P Ditty \ Documents \ SH3 \ data \ cfg \ Backups_SCR * .clg中搜索ClassName = 变量,然后如果找到,则用Class = ShipDummy替换整行。 Campaign_SCR.mis.tmp中的变量。它还用“Type = 206”替换它下面的1行。
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)变量(单词)
搜索的列表可以是外部的吗?我们可以让它每行搜索一个单词。例如:如果列表如下:
俾斯麦 引擎罩 击退
它会搜索每个单词。**
当它将列表中的单词与(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”
感谢您的时间!
顺便说一下,这可能是我在这里遇到的问题的一个更简单的解决方法:How do I apply this?
例如:
假设我有一个外部列表(例如,称之为List.txt),如下所示:
Bismarck
Hood
Repulse
我会搜索C:\ Users \ P Ditty \ Documents \ SH3 \ data \ cfg \ Backups_SCR * .clg。说它在这里找到“俾斯麦”和“兜帽”:
sadfasfasfdBismarckfasdfasdfasdfas
asdfasfdafHoodasdfasfas
然后它将搜索Campaign_SCR.mis.tmp以取代Bismarck和Hood:
Name=asdfBismarckasfdw
Class=jlkjf
Type=12
使用:
Name=ShipDummy
Class=ShipDummy
Type=206
和
Name=asdfHoodasfdw
Class=jlkjf
Type=13
使用:
Name=ShipDummy
Class=ShipDummy
Type=206
答案 0 :(得分: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