2014-10-08 72 views
0

我有一个文件每24小时更新一次,新数据添加到最后(因为它应该),但是文件开头的一些数据变得不相关。我需要的是一个批处理文件,它将删除第3行和第4行,然后使用相同的名称保存该文件。批处理文件删除特定的行号

因此,举例来说,假设该文件是file.txt的,它看起来像这样:

  1. 一个
  2. Ç
  3. d
  4. Ë
  5. ˚F

我需要第3个和第4行删除,因此该文件将是这个样子的:

  1. 一个
  2. Ë
  3. ˚F

任何帮助是极大的赞赏。

+0

你尝试过什么吗?发布一些代码。 – Rafael 2014-10-08 12:19:18

+0

我还没有,因为我没有写批处理文件的知识。我知道这是要求用勺子喂食,但我不知道从哪里开始,而且我的Google搜索都没什么帮助。 – 2014-10-08 12:24:17

回答

0

这里是批次代码通过去除线3和4

它完全注释修改文件。所以我希望你能理解它。

您需要在第五行修改要修改的文件的路径和名称。

@echo off 
setlocal EnableDelayedExpansion 

rem Define name of file to modify and check existence. 
set "FileToModify=C:\Temp\Test.tmp" 
if not exist "%FileToModify%" goto EndBatch 

rem Define name of temporary file and delete this file if it currently 
rem exists for example because of a breaked previous batch execution. 
set "TempFile=%TEMP%\FileUpdate.tmp" 
if exist "%TempFile%" del "%TempFile%" 

rem Define a line number environment variable for temporary usage. 
set "Line=0" 

rem Process the file to modify line by line whereby empty lines are 
rem skipped by command FOR and all other lines are just copied to 
rem the temporary file with the exception of line 3 and line 4. 
for /F "useback delims=" %%L in ("%FileToModify%") do (
    if !Line! GTR 3 (
     echo %%L>>"%TempFile%" 
    ) else (
     rem Increment line number up to number 4. 
     set /A Line+=1 
     rem Copy line 1 and 2, but not line 3 and 4. 
     if !Line! LSS 3 echo %%L>>"%TempFile%" 
    ) 
) 

rem Copy the temporary file being a copy of file to modify with 
rem the exception of removed line 3 and 4 over the file to modify. 
rem Finally delete the temporary file. 
copy /Y "%TempFile%" "%FileToModify%" >nul 
del "%TempFile%" 

:EndBatch 
endlocal 
相关问题