如何用批处理bat将多个txt文件中的指定字符串删除?

多个txt文件中含有多个指定字符串,如何手动输入这些字符串并将其删除。例如多个文档行内(是在文字中间,但也包括行首和行尾)含有图片中我写的字符串以及 我没写的其它字符串等等,注意图片中每行字符串都不在一起的,不知道每个字符串在哪个txt内,哪个段落的哪个行内,现在求通过手动输入这些字符串,,将其在多个txt内删除。

不清楚你的实际文件/情况,仅以问题中的样例/说明为据
以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起运行
<# :
cls
@echo off
rem 根据一个指定文本文件里列出的多个字符串,将多个txt文本文件内的这些字符串删除
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~dp0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$path=$args[0];
$newfolder=$path+"#result";
$wordsfile=$path+"deletewords.log";
if(-not (test-path -liter $wordsfile)){Write-host ('"'+$wordsfile+'" not found');exit;};
if(-not (test-path -liter $newfolder)){[void](md $newfolder)};
$files=@(dir -liter $path|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
$enc=[Text.Encoding]::Default;$n=[char]([int]'56');
$wordslist=[IO.File]::ReadAllLines($wordsfile, $enc);
for($i=0;$i -lt $files.length;$i++){
write-host $files[$i].Name;
$text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
for($j=0;$j -lt $wordslist.count;$j++){
$text=$text.replace($wordslist[$j], '');
};
if(($i+1) -le [int]$n.toString()){
$newfile=$newfolder+'\'+$files[$i].Name;
[IO.File]::WriteAllText($newfile, $text, $enc);
};
}追问

大佬,我不懂编码,要删的字符串写在哪?

追答

deletewords.log文件

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-05-19

实现你这个功能强烈推荐一个第三方命令行工具bwfr,非常小巧好用,这个工具可以根据参数文件得内容进行替换。这里有使用说明和下载地址。白杨的原创免费作品bwfr

相似回答