更改文本文件的最后20行

时间:2013-07-07 21:22:24

标签: windows batch-file cmd

如何仅更改最后20行而不是所有内容。

@echo off > newfile & setLocal enableDELAYedexpansion
copy /Y E:\LOG.DIR LOG.DIR
set old=INACTIVE
set new=ACTIVE
for /f "tokens=* delims=" %%a in (LOG.DIR) do (
set str=%%a
echo %SOON
set str=!str:%old%=%new%! >> newfile echo !str!
)

1 个答案:

答案 0 :(得分:2)

检查出来:

@ECHO OFF &SETLOCAL
copy /Y E:\LOG.DIR LOG.DIR
set "old=INACTIVE"
set "new=ACTIVE"
FOR /f %%a IN ('^<log.DIR find /v /c ""') DO SET /a length=%%a
SET /a length-=20
SETLOCAL ENABLEDELAYEDEXPANSION
<log.DIR  (
FOR /l %%a IN (1,1,%length%) DO (
    SET "line="
    SET /p "line="
    ECHO(!line!
))>newfile
ENDLOCAL
for /f "skip=%length% delims=" %%a in (LOG.DIR) do (
    set "str=%%a"
    SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=!str:%old%=%new%!"
    >>newfile ECHO(!str!
    endlocal
)

获取find行数减去20,将结果放入skip=循环中的for /f