Batch file 搜索一个文件中的单词列表,然后在另一个文件中查找这些单词,用Class=shipmdum替换整行,替换下面的两行
我想从外部列表(一行中的每个单词都很简单)中搜索一个单词列表,我们称之为“list.txt”,然后在一个文件(C:\Users\p Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg)中搜索它们(匹配整个单词,即使它在另一个文件中,并且匹配大小写),然后如果它们在另一个文件中,就可以找到这些单词(Campaign_SCR.mis.tmp)(匹配整个单词,即使它位于另一个匹配的大小写中)仅当行以“Name=”开头时,才将(Campaign_SCR.mis.tmp)中的整行替换为Name=shipmdum。之后,同一文件中该行下方的两行将替换为第2行“Class=shipmdum”,然后第3行“Type=206” 这是我现在拥有的代码。现在它在C:\Users\p Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg中搜索ClassName=Variable,然后如果在Campaign\u SCR.mis.tmp中找到变量,它会用Class=ShipDummy替换整行。它还会用“Type=206”替换它下面的一行 但我希望它从我指定的列表中搜索一个名称,在(C:\Users\p Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg)中搜索。如果它找到匹配项,则在Campaign\u SCR.mis.tmp中搜索该名称,并替换整行,前提是该行以“name=”开头,名称为ShipDummy。下面的两行将替换为第2行“Class=ShipDummy”,然后是第3行“Type=206”:Batch file 搜索一个文件中的单词列表,然后在另一个文件中查找这些单词,用Class=shipmdum替换整行,替换下面的两行,batch-file,batch-processing,batch-rename,Batch File,Batch Processing,Batch Rename,我想从外部列表(一行中的每个单词都很简单)中搜索一个单词列表,我们称之为“list.txt”,然后在一个文件(C:\Users\p Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg)中搜索它们(匹配整个单词,即使它在另一个文件中,并且匹配大小写),然后如果它们在另一个文件中,就可以找到这些单词(Campaign_SCR.mis.tmp)(匹配整个单词,即使它位于另一个匹配的大小写中)仅当行以“Name=”开头时,才将(Campaign_SCR.mis
Name=ShipDummy
Class=ShipDummy
Type=206
Name=ShipDummy
Class=ShipDummy
Type=206
**请记住,在Campaign_SCR.mis.tmp中,变量的行通常不会显示为Name=variable。它可能是Name=(words)variable(words)
搜索的列表可以是外部的吗?我们可以让它每行搜索一个单词吗。对于exmaple:如果列表如下所示:
俾斯麦
罩
击退
它将搜索每个单词**
当它将列表中的单词与(C:\Users\p Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg)匹配时,请确保它也与大小写匹配。因此列表中的“俾斯麦”将从(C:\Users\p Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg)中找到“俾斯麦”,而不会找到“俾斯麦”
谢谢你抽出时间
顺便说一句,这可能是解决我这里的问题的一个更简单的方法:
例如:
Name=ShipDummy
Class=ShipDummy
Type=206
Name=ShipDummy
Class=ShipDummy
Type=206
假设我有一个外部列表(例如称之为list.txt),如下所示:
Bismarck
Hood
Repulse
我将搜索C:\Users\p Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg查找该文件。假设它在其中找到“俾斯麦”和“胡德”:
然后,它将在Campaign_SCR.mis.tmp中搜索俾斯麦和发动机罩更换:
Name=asdfBismarckasfdw
Class=jlkjf
Type=12
与:
及
与:
@echo关闭
setlocal EnableDelayedExpansion
REM初始化要搜索的单词列表
设置targetWords=:EOF
rem我想从外部列表中搜索一个单词列表(一行中的每个单词都很简单)
对于(List.txt)中的/F%%a,请执行以下操作(
rem并在文件中搜索它们(C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg)
findstr“%%a”C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg”>NUL
如果他们在那里。。。
如果!错误级别!等于0(
REM在目标列表中插入单词
设置targetWords=!targetWords!%%a
)
)
REM在文件中插入文件结束标记
echo:EOF>>活动\u SCR.mis.tmp
REM初始化重定向活动\u SCR.mis.tmp中最后处理的行数
设置lastLine=0
rem…在另一个文件(Campaign_SCR.mis.tmp)中查找这些单词
活动\u SCR.mis.tmp.NEW
REM更新新文件
REM del Campaign_SCR.mis.tmp
REM ren活动\u SCR.mis.tmp.新活动\u SCR.mis.tmp
Wow,太快了!我要测试一下并报告。现在,如果我将“C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups\u SCR*.cfg”以及“C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg”添加到“C:\Uses\P Ditty\Documents\SH3\data\cfg\Backups\u SCR*.clg”中会怎么样我是否在脚本中的两个.clg实例旁边插入完整的文件名,然后它将搜索这两个文件?我更改了用户名,方法是几乎得到它。它更改了一个,但其余的则表示ECHO已关闭
Name=asdfHoodasfdw
Class=jlkjf
Type=13
Name=ShipDummy
Class=ShipDummy
Type=206
@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