用批处理删除指定文件中字符串前面的文字?
怎么删除指定字符串之前的内容我在论坛上搜到了删除指定字符串之后的代码,要删指定字符串之前的要怎么改。谢谢。@echooffset"str=UID"for/f"delims...
怎么删除指定字符串之前的内容
我在论坛上搜到了删除指定字符串之后的代码,要删指定字符串之前的要怎么改。谢谢。
@echo off
set "str=UID"
for /f "delims=" %%s in ('findstr /m /c:"%str%" *.txt') do call :sub "%%s"
del "%temp%\.tmp"
exit /b
:sub
findstr /n .* %1 > "%temp%\.tmp"
(
for /f "usebackq delims=" %%t in ("%temp%\.tmp") do (
EndLocal
set var=%%t
SetLocal EnableDelayedExpansion
if "!var:%str%=!" neq "!var!" (
for /f "delims=" %%L in ("!var:%str%=!") do set var=%%L
echo,!var:*:=!
goto :eof
)
echo,!var:*:=!
)
) > %1 展开
我在论坛上搜到了删除指定字符串之后的代码,要删指定字符串之前的要怎么改。谢谢。
@echo off
set "str=UID"
for /f "delims=" %%s in ('findstr /m /c:"%str%" *.txt') do call :sub "%%s"
del "%temp%\.tmp"
exit /b
:sub
findstr /n .* %1 > "%temp%\.tmp"
(
for /f "usebackq delims=" %%t in ("%temp%\.tmp") do (
EndLocal
set var=%%t
SetLocal EnableDelayedExpansion
if "!var:%str%=!" neq "!var!" (
for /f "delims=" %%L in ("!var:%str%=!") do set var=%%L
echo,!var:*:=!
goto :eof
)
echo,!var:*:=!
)
) > %1 展开
展开全部
楼主问了两个次哦,怎么也没别人来答,那我就再改写一个吧,处理会保留空行、查找不分大小写 (他这个删指定字符之下的代码要是文件名有空格会出错)
@echo off&SetLocal EnableDelayedExpansion
set "str=UID"
for /f "delims=" %%s in ('findstr /m /i /c:"%str%" "*.txt"')do (
set flag=
for /f "tokens=1* delims=:" %%u in ('findstr /n .* "%%s"') do (
set var=%%u:%%v
if "!var:%str%=!" neq "!var!" (
for /f "delims=" %%L in ("!var:%str%=!")do (
call echo.!var:%%L=!>ykytemp.txt
set flag=1
)
)else (if defined flag echo.%%v>>ykytemp.txt)
)
del "%%s"
ren ykytemp.txt "%%s"
)
echo 处理完毕,即将退出...
ping /n 5 127.1>nul
@echo off&SetLocal EnableDelayedExpansion
set "str=UID"
for /f "delims=" %%s in ('findstr /m /i /c:"%str%" "*.txt"')do (
set flag=
for /f "tokens=1* delims=:" %%u in ('findstr /n .* "%%s"') do (
set var=%%u:%%v
if "!var:%str%=!" neq "!var!" (
for /f "delims=" %%L in ("!var:%str%=!")do (
call echo.!var:%%L=!>ykytemp.txt
set flag=1
)
)else (if defined flag echo.%%v>>ykytemp.txt)
)
del "%%s"
ren ykytemp.txt "%%s"
)
echo 处理完毕,即将退出...
ping /n 5 127.1>nul
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询