第1个回答 2008-12-25
::用指定字符替换指定内容.cmd
@echo off
setlocal enabledelayedexpansion
set file=
set /p file= 请输入要操作的文件名称(包括扩展名):
set "file=%file:"=%"
for %%i in ("%file%") do set file=%%~fi
echo.
set replaced=
set /p replaced= 请输入即将被替换的内容:
echo.
set all=
set /p all= 请输入替换字符串:
for /f "delims=" %%i in ('type "%file%"') do (
set str=%%i
set "str=!str:%replaced%=%all%!"
echo !str!>>"%file%"_tmp.txt
)
copy "%file%" "%file%"_bak.txt >nul 2>nul
move "%file%"_tmp.txt "%file%"
start "" "%file%"
@echo off
:: 删除重复的行,但不能保留空行
:: 对不符合变量命名规则、变量个数超过限制的文本都无法正确处理
:: code by youxi01 modified by jm 2006-10-31
(echo 清除重复行后的文件内容:& echo.)>str_.txt
for /f "delims=" %%i in (test.txt) do (
if not defined %%i set %%i=A & echo %%i>>str_.txt)
start str_.txt