Trim Last Line

  • Thread starter Thread starter Barney
  • Start date Start date
B

Barney

I have a text file of variable length whose last line I
would like to programmatically delete. How can this be
done (preferably without any third party programs)?
 
Barney said:
I have a text file of variable length whose last line I
would like to programmatically delete. How can this be
done (preferably without any third party programs)?

This will do, but keep in mind that the handling of some special chars
may leed to undesired results.

Pass the filename as argument, output may be redirected to another file.

::StripLast.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
if not exist %1 goto :eof
for /f %%A in ('find /V /C "" ^<%1') do set lines=%%A
for /f "tokens=1,* delims=[]" %%A in ('find /V /N "" ^<%1') do (
if %%A LSS %lines% echo %%B
)
::StripLast.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
 
Matthias Tacke said:
This will do, but keep in mind that the handling of some special chars
may leed to undesired results.

Pass the filename as argument, output may be redirected to another file.
Sorry forgot the empty lines delimiter, change the space between echo
and %%B to a slash.
::StripLast.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
if not exist %1 goto :eof
for /f %%A in ('find /V /C "" ^<%1') do set lines=%%A
for /f "tokens=1,* delims=[]" %%A in ('find /V /N "" ^<%1') do (
if %%A LSS %lines% echo %%B

if %%A LSS %lines% echo/%%B
)
::StripLast.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
HTH
 
Great, thanks Matthias.
-----Original Message-----
Matthias Tacke said:
This will do, but keep in mind that the handling of some special chars
may leed to undesired results.

Pass the filename as argument, output may be redirected to another file.
Sorry forgot the empty lines delimiter, change the space between echo
and %%B to a slash.
::StripLast.cmd::::::::::::::::::::::::::::::::::::::::: :::::::::::::
@echo off
setlocal
if not exist %1 goto :eof
for /f %%A in ('find /V /C "" ^<%1') do set lines=%%A
for /f "tokens=1,* delims=[]" %%A in ('find /V /N "" ^<% 1') do (
if %%A LSS %lines% echo %%B

if %%A LSS %lines% echo/%%B
)
::StripLast.cmd::::::::::::::::::::::::::::::::::::::::: :::::::::::::
HTH

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
.
 
I'm very impressed with the script. But since
shell scripting can become unreadable, *please*
add some comments-
::
:: Thanks in advance
::

- Bill

Matthias Tacke said:
Matthias Tacke said:
This will do, but keep in mind that the handling of some special chars
may leed to undesired results.

Pass the filename as argument, output may be redirected to another file.
Sorry forgot the empty lines delimiter, change the space between echo
and %%B to a slash.
::StripLast.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
if not exist %1 goto :eof
for /f %%A in ('find /V /C "" ^<%1') do set lines=%%A
for /f "tokens=1,* delims=[]" %%A in ('find /V /N "" ^<%1') do (
if %%A LSS %lines% echo %%B

if %%A LSS %lines% echo/%%B
)
::StripLast.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
HTH

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
William Hymen said:
I'm very impressed with the script. But since
shell scripting can become unreadable, *please*
add some comments-
::
:: Thanks in advance
::
I find it quite readable, but it requires detailed knowledge of find
and for :-)

The very much enhanced "for" makes this quite compact batch possible.
Read the help "for /?" several times and play around with the options
for some time to get used to them.

Reading here in mpwca and in alt.msdos.batch.nt will show that "for" is
among the most often used and helpful commands.

::StripLastLine.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal
:: check if file exists
if not exist %1 goto :eof

:: Use find /C to count all lines and store in variable %lines%
:: option /V reverses the reslt of a nul search and even gets
:: lines with only crlf, uses redirection to avoid file name output.
for /f %%A in ('find /V /C "" ^<%1') do set lines=%%A

:: similar to previous, find option /N precedes every line with the
:: number in square brackets. For reads the number to %%A the
:: line contents to %%B.
for /f "tokens=1,* delims=[]" %%A in ('find /V /N "" ^<%1') do (

:: Output line only if not last one.
:: Use a char different from space after echo to avoid the secho staus
:: being reporting in case %%B is empty.
:: You may do other number comparisons to get different ranges
if %%A LSS %lines% echo/%%B
)
::StripLastLine.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
 
Back
Top