我需要使用 Batch 来检查文件 Directory.twml 以查看它是否包含文件 Blocked.twml 中的任何单词以及它是否替换为 [Blocked]
这是两个文件的示例:
目录.twml
11:38:38.90 [System] Twml Has joined the chat.
11:38:41.17 [User] Twml says: line one
11:38:42.96 [User] Twml says: line bad two
11:38:46.27 [User] Twml says: line three
11:38:50.16 [User] Twml says: you get the idea here
11:38:52.35 [System] Twml Has logged off.
被阻止.twml
word1
word2
word3
bad
word5
word6
我想要 Directory.twml 所说的内容是
11:38:38.90 [System] Twml Has joined the chat.
11:38:41.17 [User] Twml says: line one
11:38:42.96 [User] Twml says: line [Blocked] two
11:38:46.27 [User] Twml says: line three
11:38:50.16 [User] Twml says: you get the idea here
11:38:52.35 [System] Twml Has logged off.
我已经可以使用 Findstr 在文件中查找并查看文本是否存在,但据我所知,我不需要检查设置的单词,而是检查文件 Blocked.twml 中的单词列表
findstr /i "bad" <"Directory.twml" 1>nul
我也可以从文件中删除该单词,但我想替换它而不仅仅是删除
findstr /i /v /c:"%text%" Directory.twml > "Directory.twmll" 2>nul
del Directory.twml /s /a >nul
copy Directory.twmll Directory.twml >nul
attrib +h Directory.twml
del Directory.twmll /s /a >nul
但这又是一组文本,不是从文件中的列表中查找的
如果 Directory.twml 包含 Blocked.twml 中的任何内容,请替换为 [Blocked] 但我不知道该怎么做
=========编辑===========
这是解决方案:
(
for /f "delims=" %%A in (Directory.twml) do (
set "line=%%A"
for /f "delims=" %%B in (blocked.twml) do set "line=!line: %%B = [Blocked] !"
echo !line!
)
)>Directory.new
它的输出对我来说是这样的
13:22:14.16 [User] twml says: this is a test
13:22:20.37 [User] twml says: this is a [Blocked] word test