从txt删除前5行和后2行

时间:2017-11-16 16:18:46

标签: windows batch-file

需要使用bat删除txt文件的前5行和最后2行,尝试使用适用于win 10但不适用于win server 2012 r2的脚本。

知道为什么吗?

继承剧本,我有

@echo off
setlocal EnableDelayedExpansion

set "FileToModify=getdoc.txt"
 if not exist "%FileToModify%" goto:leavenow

 set "TempFile=%TEMP%\FileUpdate.txt"
 if exist "%TempFile%" del "%TempFile%"
 if exist newFile.txt del newFile.txt
 if exist Testfinal.txt del Testfinal.txt
 if exist Test1.txt del Test1.txt
 if exist myOriginalFile.txt del myOriginalFile.txt

set "Line=0"
for /F "useback delims=" %%L in ("%FileToModify%") do (
if !Line! GTR 4 (
    echo %%L>>"%TempFile%"
) else (
    set /A Line+=1
    if !Line! GTR 4 echo %%L>>"%TempFile%"
)
)
copy /Y "%TempFile%" "%FileToModify%" >nul

set row=
  for /F "delims=" %%j in (%FileToModify%) do (
if  defined row echo.!row!>>Test1.txt
set row=%%j
   )

set row=
for /F "delims=" %%j in (Test1.txt) do (
 if  defined row echo.!row!> Testfinal.txt
 set row=%%j
   )

 echo { > newFile.txt
 type %FileToModify% >> newFile.txt
 type newFile.txt > getdoc.txt

 del FileUpdate.txt
 del newFile.txt
 del Testfinal.txt
 del Test1.txt

 :leavenow
 echo no file to clean!!!!
 pause
 exit

确定有一种简单的方法可以做到这一点,谢谢

1 个答案:

答案 0 :(得分:0)

我建议使用以下注释批处理文件执行此任务:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "FileToModify=getdoc.txt"
if not exist "%FileToModify%" (
    set "ErrorMessage=No file "%FileToModify%" to clean"
    goto ErrorOutput
)

rem Get number of lines in file to modify by searching with FINDSTR for begin
rem of line which is true for every line in the file. Output by FINDSTR is the
rem line number a colon and the line itself for each line in file. For interest
rem is here just the line number left to first colon from last output line.

for /F "delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%FileToModify%"') do set "LastLineNumber=%%I"

rem Has the file at least 8 lines to remove five lines at top and two
rem lines at bottom and have finally a file with at least one line?

if %LastLineNumber% LSS 8 (
    set "ErrorMessage=File "%FileToModify%" has less than 8 lines"
    goto ErrorOutput
)

rem Decrement the number of last line because the
rem last two lines should be removed from the file.

set /A LastLineNumber-=1

rem Derive name of temporary file to create in folder for temporary files
rem from name of file to modify by replacing its file extension by "tmp".

for %%I in ("%FileToModify%") do set "TempFile=%TEMP%\%%~nI.tmp"

rem Make sure the temporary file does not already exist from a previous
rem run of this batch file broke during processing by command interpreter.
del "%TempFile%" 2>nul

for /F "skip=5 tokens=1* delims=:" %%I in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%FileToModify%"') do (
    if %%I == %LastLineNumber% goto ReplaceFile
    echo/%%J>>"%TempFile%"
)

:ReplaceFile
move /Y "%TempFile%" "%FileToModify%" 2>nul
if errorlevel 1 (
    del "%TempFile%"
    set "ErrorMessage=File "%FileToModify%" is write-protected"
    goto ErrorOutput
)
endlocal
exit /B

:ErrorOutput
cls
echo %~f0
echo/
echo Error: %ErrorMessage%.
echo/
pause

注意:此批处理文件无法为包含以1个或更多: 开头的行的文件生成正确的输出,因为在第二个 FOR 选项delims=:会导致分配给循环变量I FINDSTR 的行号输出中的这些行以及分配给1个或多个冒号后的所有内容循环变量J是指定循环变量I之后ASCII表中的下一个字符。

但是这个批处理文件处理包含命令行关键字符的文件,例如&()[]{}^=;!'+,`~|<>

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • call /? ...解释%~f0(包含文件扩展名和完整路径的批处理文件的名称)。
  • cls /?
  • del /?
  • echo /?
  • endlocal /?
  • exit /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

请参阅DosTips论坛主题ECHO. FAILS to give text or blank line - Instead use ECHO/,了解批量文件中使用echo/代替echo.的原因。