title: Trim a text file via a batch file
body: <p>I want to trim a text file from both top and bottom.
  Say if the text file contain 90 lines. I want to delete top x lines and bottom y lines and update the file. (ie leave only the range x+1 to y-1.
  Hence I to intend write a batch file and run it as follow.</p>
  
  <pre><code>trimmer.bat file1.txt 2 8
  </code></pre>
  
  <p>This code below has following limitations
  (a) file open/amend twice
  (b) Trims blank lines within the range (I do not wish to remove these lines)</p>
  
  <p>Please help me to correct</p>
  
  <pre><code>@echo off
  rem %1% file name  %2%range begins   %3%range ends
  
  rem Step1: this code trims the bottom section
  set /a bot_trim=%3
  echo Removing...
  for /f "skip=%bot_trim% delims=*" %%a in (%1) do (
  echo %%a &gt;&gt;newfile.txt
  ) &gt;nul
  echo Lines removed, rebuilding file...
  xcopy newfile.txt %1 /y &gt;nul
  echo File rebuilt, removing temporary files
  del newfile.txt /f /q &gt;nul
  echo file bottom trimmed...
  
  rem Step2: this code trims the Top section
  set /a Top_trim=%2-1
  echo Removing...
  for /f "skip=%Top_trim% delims=*" %%a in (%1) do (
  echo %%a &gt;&gt;newfile.txt
  ) &gt;nul
  echo Lines removed, rebuilding file...
  xcopy newfile.txt %1 /y &gt;nul
  echo File rebuilt, removing temporary files
  del newfile.txt /f /q &gt;nul
  echo file bottom trimmed...
  echo file trim completed !!
  pause
  </code></pre>
  
  <p>contents of file1.txt</p>
  
  <pre><code>aa
  ff
  ff
  
  dd
  dd
  
  ee
  ee
  ee
  </code></pre>