Board logo

标题: [文本处理] 批处理怎么替换带*字符的文本内容? [打印本页]

作者: xp3000    时间: 2013-9-22 15:12     标题: 批处理怎么替换带*字符的文本内容?

比如网站复制一些文章,一些内容被打成了**
把他保存在C盘下的TXT文件夹,有多个TXT文件,把带*内容替换成需要的,
***异常替换为服务器异常
操作**替换为操作系统
这个无法替换带**的内容,怎么弄?
  1. @echo off
  2. setlocal disabledelayedexpansion
  3. title 修改中...
  4. rem 设置
  5. set "dir=C:\TXT"
  6. set replace_list="***异常=服务器异常" "操作**=操作系统"
  7. rem 输出文件列表
  8. dir /s /b /a:-d-r "%dir%\*.txt" >..tmp || goto exit
  9. rem 统计文件个数
  10. for /f %%i in (' "find /c /v "" <..tmp" ') do (
  11.     set /a n_total = %%i, n_current = 0
  12. )
  13. rem 替换
  14. set "s=                       "
  15. for /f "delims=" %%i in (..tmp) do (
  16.     set /a n_current += 1
  17.     findstr /n .* "%%i" >..tmp
  18.     (
  19.         for /f "delims=" %%a in (..tmp) do (
  20.             set str=%%a
  21.             setlocal enabledelayedexpansion
  22.             set /p =%s: =%!n_current! / %n_total% <nul >con
  23.             set str=!str:*:=!
  24.             for %%a in (%replace_list%) do (
  25.                 if defined str set str=!str:%%~a!
  26.             )
  27.             echo,!str!
  28.             endlocal
  29.         )
  30.     ) >"%%i"
  31. )
  32. :exit
  33. title 完成
  34. del ..tmp 2>nul
  35. echo,
  36. echo 按任意键退出...
  37. pause >nul
复制代码

作者: PowerShell    时间: 2013-9-22 18:45

  1. PS C:\Users\user001\desktop\powershell> "***异常" -replace "\*\*\*异常","服务器异常"
  2. 服务器异常
  3. PS C:\Users\user001\desktop\powershell> "****异常" -replace "\*\*\*异常","服务器异常"
  4. *服务器异常
  5. PS C:\Users\user001\desktop\powershell>
复制代码





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