0

I have a rather specialized task I am trying to automate. Each day, someone has to copy 144 each from four different .dat files into an Excel spreadsheet. These .dat files are updated twice daily with new data resulting in the file growing each time. So we copy 144 lines near the bottom of the file with the last line to be copied a known number of lines from the final line in the file. I would like to automate this process. I have determined a way to do this, but I am a novice with coding. I would like to write a script that can copy the 144 lines a specified number of lines (say 22) from the end of the file, and paste into a new text file. I have been able to find some information on copy and pasting the first 100 lines of a text file into a new file (http://www.computing.net/answers/windows-xp/dos-batch-to-copy-first-100-lines-of-a-text-file/202404.html) and some information on displaying the last 10 lines (CMD.EXE batch script to display last 10 lines from a txt file), but I have found nothing that seems like it would work for this.

I have this for copying the last n lines:

    @echo OFF

    :: Get the number of lines in the file
    for /f %%i in ('find /v /c "" ^< test.dat') do set /a lines=%%i

    :: Paste last 10 lines
    set /a startLine=%lines% - 10
    more /e +%startLine% ForneyTEOM.dat > lastLines.txt

    echo All Done.
    Echo.
    Echo Press any key to close this window.
    Pause>NUL
    EXIT

This doesn't let me take the n lines that end x lines from the end of the file and start x+n lines from the end of the file which is what I want.

Community
  • 1
  • 1
A. Long
  • 1
  • 2
  • Your life will be a lot easier if you can use powershell as it supports loading the content of a file and then using indexed based referencing to pick the lines you're interested in. Read up on get-content and out-file in powershell. – Levesque Oct 06 '15 at 14:55
  • 1
    Please share what you have tried so far and describe precisely where you are stuck; remember that SO is not a free code writing service... – aschipfl Oct 06 '15 at 14:56
  • 1
    Batch can not work with excel files; Only `.csv` would be possible, as it's pure text. – Stephan Oct 06 '15 at 15:18
  • You already linked [the solution](http://stackoverflow.com/a/1632394/5047996) in your question; so what does still not work? – aschipfl Oct 06 '15 at 18:28
  • I don't need to interface with Excel. I was just trying to explain my overall goal. I have a batch file that can copy and paste any number of lines that I set starting at the beginning of the file. I have another that can copy and paste any number I set at the end of the file. My problem is that I need 144 lines that reside 22 lines from the end. I will update my original post. – A. Long Oct 07 '15 at 14:50
  • Couldn't you use a two stage process - copy the last x+n lines into a temporary file. Then copy the first x lines from that file into your target file, and delete the temporary file? – lessthanideal Oct 07 '15 at 15:34
  • The two stage process is less than ideal, but it does work. Thanks. Not sure why I didn't think of that. – A. Long Oct 09 '15 at 17:44

0 Answers0