我在同一文件夹中有 10 个文本文件,其列表如下:
abc0012345
abc0012346
abc0012347
abc0012348
abc0012349
abc0012350
abc0012351
abc0012352
我有一个remove.txt文件,其中包含我要删除的字符串,它包含abc0012347
和abc0012351
,我还想删除它们留下的空格
通过下面的代码,我可以列出是否可以在 10 个文本文件中找到字符串,但我在删除字符串时遇到问题
:: /i `: Specifies that the search is not to be case-sensitive.
:: /g: file `: Gets search strings from the specified file.
Rem "List of systems before removing from tag files"
echo off
c:
cd C:\StringFiles
findstr /I /G:"remove.txt" *.txt
pause
rem the following line removes specific entry from text file but I need it to look in the remove.txt file for the strings to remove and then loop through all text files
rem type C:\E\TagFiles\2.txt | repl "stc0012345+" "" X | repl "\r?\n?\s*$" "" M > C:\E\TagFiles\2.out.txt`
最佳答案
如果您的 FINDSTR 命令正确地仅列出了应删除的行,那么您所需要的只是 /V
选项来打印所有不匹配的行。
您还需要一个 FOR
循环来迭代文件。您可以将保留的内容写出到新文件中,然后使用 MOVE
将原始内容替换为新文件。您还需要确保不修改“remove.txt”。
for %%F in (*.txt) do if /i "%%F" neq "remove.txt" (
findstr /vig:"remove.txt" "%%F" >"%%F.new"
move /y "%%F.new" "%%F" >nul
)
REPL.BAT不太适合解决此问题,因为它没有内置功能来加载搜索或替换其他文件中的字符串。
如果您喜欢使用 REPL.BAT 执行其他任务,那么我建议您升级到较新的 JREPL.BAT公用事业。它具有 REPL.BAT 的所有功能,此外还有更多功能。然而,对于这个问题来说,它仍然不是一个好的选择。
关于batch-file - .bat 从多个 .txt 文件中删除字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36826961/