Delete last line in txt file using batch?

Score
0
Vote Up
February 13, 2012 at 11:42:20 Pacific
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.

Reply ↓  Report •


#1
Vote Down
Score
2
Vote Up
February 13, 2012 at 13:21:12 Pacific

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


Reply ↓  Report •

#2
Vote Down
Score
0
Vote Up
February 13, 2012 at 17:16:52 Pacific

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
)


Reply ↓  Report •

#3
Vote Down
Score
0
Vote Up
February 13, 2012 at 18:49:02 Pacific

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"

Reply ↓  Report •

#4
Vote Down
Score
1
Vote Up
February 14, 2012 at 07:18:26 Pacific

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.

Reply ↓  Report •

Reply to Message Icon Start New Discussion
Related Posts

« Batch Script to Read Line... [Solved] Robocopy With User Prompt... »