Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!
Delete last line: http://www.cn-dos.net/forum/viewthread.php?ti ...
Modification of this text!
The code written before did not achieve the desired effect!
Because, if the last line is a pure carriage return, it still exists when output.
This program has been slightly modified on the original basis to achieve the effect!
Everyone can test!
@echo off
setlocal EnableDelayedExpansion
rem Delete temporary files.
if exist result*.txt del result*.txt
rem Calculate the number of lines, considering blank lines.
copy test.txt tt.txt
echo __>>tt.txt
for /f "delims= skip=2" %%i in ('find /v /n "" tt.txt') do set /a line+=1
::set/a num=line
del tt.txt
echo The number of lines in test.txt is: !line!.
pause>nul
rem The following is to output the text content after removing the last line, but several temporary files are used for transfer.
for /f "tokens=1,2* delims=" %%i in ('findstr /x /n .* test.txt') do (
set /a l+=1
if "!l!" neq "!line!">>result0.txt echo %%i
)
set /a l=line-2
set /a ll=line-1
for /f "tokens=1,* delims=:" %%i in (result0.txt) do (
rem Originally here was 1,2*
set /a n+=1
::if "!n!" neq "!line!" ( if not "%%j"=="" (echo %%j>>result.txt) else echo.>>result.txt)
if !n! leq !l! ( if not "%%j"=="" (echo %%j>>result.txt) else (echo.>>result.txt))
if !n! equ !ll! set/p a=%%j>>result.txt<nul
)
rem Delete the unnecessary temporary file result0.txt, only keep the final result.
del result0.txt
Finished!
Improve it once more, it should be correct!
[ Last edited by scriptor on 2007-9-15 at 01:26 PM ]
Credits 2,404 Posts 946 Joined 2005-09-08 13:44 20-year member UID 42345
Status Offline
Is this simpler?
@echo off
cd.>test2.txt
for /f "delims=:" %%i in ('findstr /n . test.txt') do set n=%%i
set /a nm=%n%
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a mn=%%i 2>nul
set "m=%%i"
setlocal enabledelayedexpansion
set "m=!m:*:=!"
if !nm! EQU !mn! goto end
echo.!m!>>test2.txt
endlocal
)
:end
pause
Credits 1,187 Posts 555 Joined 2006-12-21 07:35 19-year member UID 74129 Gender Male
Status Offline
Tested.There is a problem that certain characters in lines containing! cannot be processed. For example,!#$%@. The! will be omitted in the output because setlocal is in effect!
Credits 1,187 Posts 555 Joined 2006-12-21 07:35 19-year member UID 74129 Gender Male
Status Offline
Originally posted by terse at 2007-9-15 13:38:
Is this simpler?
@echo off
cd.>test2.txt
for /f "delims=:" %%i in ('findstr /n . test.txt') do set n=%%i
set /a nm=%n%
for /f "delims=" %%i in ('findstr /n . ...
Not bad a
There are problems:
1: The last line has an extra blank line!
2: When there are blank lines in the middle of the text, it cannot be handled well
For example:
1
2
3
....
Your code output is:
1
2
3
.....
And there is an extra blank line at the end
[ Last edited by scriptor on 2007-9-15 at 02:23 PM ]
Credits 2,404 Posts 946 Joined 2005-09-08 13:44 20-year member UID 42345
Status Offline
Everything here is fine. I don't know how you tried it. Finally, there are indeed extra blank lines, but blank lines can be handled.
The content of test.txt is as follows:
@echo off
cd.>test2.txt
for /f "delims=:" %%i in ('findstr /n . test.txt') do set n=%%i
set /a nm=%n%
for /f "delims=" %%i in ('findstr /n .* test.txt') do (
set /a mn=%%i 2>nul
set "m=%%i"
setlocal enabledelayedexpansion
set "m=!m:*:=!"