批处理文件放你那当前目录下,假定要找的字符串是abcd,运行后将把在此之前的所有内容(包括本行之前的及上面所有行)删去,未找到这字符串的文本则不动:
@echo on&setlocal enabledelayedexpansion
set str=abcd
for /f "delims=" %%i in ('findstr/m /i "%str%" "*.txt"')do (
for /f "tokens=1* delims=:" %%j in ('findstr/n /i "%str%" "%%i"')do (
set hh=%%k
set hh=!hh:%str%=■!
set n=0
call :cot
echo.!hh!>"%%i.yky"
more +%%j "%%i">>"%%i.yky"
del "%%i"
ren "%%i.yky" "%%i"
))
pause
exit
:cot
call set hhx=%%hh:~%n%,1%%
if not "!hhx!"=="■" (
set/a n+=1
goto cot )
call set hh=%%hh:~%n%%%
set hh=!hh:■=%str%!
echo.!hh!
goto :eof
温馨提示:答案为网友推荐,仅供参考