Delete last line in txt file using batch?

February 13, 2012 at 11:42:20
Specs: Windows 7
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.

See More: Delete last line in txt file using batch?

Report •

#1
February 13, 2012 at 13:21:12
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


Report •

#2
February 13, 2012 at 17:16:52
Never mind all this stuff I figured it out that i need:
:code
set cnt=0
echo.
for /F "delims=" %%j in (%class%.txt) do (
  set /A cnt+=1
  set line!cnt!=%%j
)
set cde=0
:codeext
set /a cde+=1
echo.!line%cde%!
if not %cde% == %cnt% (
goto codeext
) else (
echo.
goto home
)


Report •

#3
February 13, 2012 at 18:49:02
FYI the delete line code doesn't do anything! I think that you thought i said make new line of code but i didn't! I said delete last line. I already know how to make a line:
echo.Text and stuff.>>"txtfile.txt"

Report •

Related Solutions

#4
February 14, 2012 at 07:18:26
You did not try or understand my code to delete the last line of a text file:

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

The above just deletes the last line since it stores and delays the write operation holding a line waiting.
You know how to make a line, but you need to study the For /F behavior in depth.

Report •

Ask Question