Board logo

标题: [文本处理] 批处理怎么给指定字符串后加指定字符? [打印本页]

作者: 鲁班小仙    时间: 2022-8-19 10:41     标题: 批处理怎么给指定字符串后加指定字符?

1、我一个文件夹里面有多个.TXT文件
2、每个TXT文件里面有多行内容
3、我想给指定的字符串后面加指定字符指定的字符串放在一个TXT里面进行检测,在TXT里面就加,不在就不加,指定字符是一样的
4、请问是否可以用BAT实现?

QQ:1181180827 红包感谢
作者: zaqmlp    时间: 2022-8-19 11:16

  1. <# :
  2. cls&echo off&cd /d "%~dp0"&rem bat存为ANSI/GB2312编码
  3. set "current=%cd%"
  4. powershell -NoProfile -ExecutionPolicy bypass "Get-Content -literal '%~f0'|Out-String|Invoke-Expression"
  5. pause
  6. exit
  7. #>
  8. $findword=@'
  9. 指定的字符串1
  10. 指定的字符串2
  11. '@;
  12. $suffix='添加字符';
  13. $arr=$findword.trim("`r`n") -split '[\r\n]+';
  14. $current=$env:current;
  15. $enc=[Text.Encoding]::GetEncoding('GB2312');
  16. $files=@(dir -literal $current|?{('.txt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
  17. for($i=0;$i -lt $files.length;$i++){
  18.     write-host $files[$i].FullName.Substring($current.length+1);
  19.     $text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
  20.     for($j=0;$j -lt $arr.length;$j++){
  21.         $newword=$arr[$j]+$suffix;
  22.         $text=$text.replace($arr[$j], $newword);
  23.     }
  24.     [IO.File]::WriteAllText($files[$i].FullName, $text, $enc);
  25. }
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2