computing
  • 2

Delete Last Line In Txt File Using Batch?

  • 2

I actually have 2 questions.
1: I need to delete the LAST line in a txt file! NOT the first line!! How do i do this?
2: I need to read the entire file and set each line to a variable (line1,line2,line3,line4,line5…..) I was gonna use a var called var for the number ex.
set line%var%=<Insert text here>
set /a var+=1
3: I also want it to goto something then return to the code. Like read one line then set the var then goto some_place then do some stuff then go back to the code to read yet another line.
4: It ALSO needs to stop once the entire txt file is read.

Share

1 Answer

  1. First question

    @echo off & setlocal EnableDelayedExpansion
    set row=
    for /F "delims=" %%j in (File.txt) do (
      if  defined row echo.!row!>> File.new
      set row=%%j
    )
    

    Second question

    @echo off & setlocal EnableDelayedExpansion
    set cnt=0
    for /F "delims=" %%j in (File.txt) do (
      set /A cnt+=1
      set line!cnt!=%%j
      call :SUB
    )
    exit
    
    ;SUB
      [HERE YOUR STUFF]
    exit
    

    • 0