2014-10-08 71 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 
相關問題