标题: [文本处理] 批处理怎么替换带*字符的文本内容? [打印本页]
作者: xp3000 时间: 2013-9-22 15:12 标题: 批处理怎么替换带*字符的文本内容?
比如网站复制一些文章,一些内容被打成了**
把他保存在C盘下的TXT文件夹,有多个TXT文件,把带*内容替换成需要的,
***异常替换为服务器异常
操作**替换为操作系统
这个无法替换带**的内容,怎么弄?- @echo off
- setlocal disabledelayedexpansion
- title 修改中...
-
- rem 设置
- set "dir=C:\TXT"
- set replace_list="***异常=服务器异常" "操作**=操作系统"
-
- rem 输出文件列表
- dir /s /b /a:-d-r "%dir%\*.txt" >..tmp || goto exit
-
- rem 统计文件个数
- for /f %%i in (' "find /c /v "" <..tmp" ') do (
- set /a n_total = %%i, n_current = 0
- )
-
- rem 替换
- set "s= "
- for /f "delims=" %%i in (..tmp) do (
- set /a n_current += 1
- findstr /n .* "%%i" >..tmp
- (
- for /f "delims=" %%a in (..tmp) do (
- set str=%%a
- setlocal enabledelayedexpansion
- set /p =%s: =%!n_current! / %n_total% <nul >con
- set str=!str:*:=!
- for %%a in (%replace_list%) do (
- if defined str set str=!str:%%~a!
- )
- echo,!str!
- endlocal
- )
- ) >"%%i"
- )
-
- :exit
- title 完成
- del ..tmp 2>nul
- echo,
- echo 按任意键退出...
- pause >nul
复制代码
作者: PowerShell 时间: 2013-9-22 18:45
- PS C:\Users\user001\desktop\powershell> "***异常" -replace "\*\*\*异常","服务器异常"
- 服务器异常
- PS C:\Users\user001\desktop\powershell> "****异常" -replace "\*\*\*异常","服务器异常"
- *服务器异常
- PS C:\Users\user001\desktop\powershell>
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |