How can I delete the last character of the text file

P

Phil Robyn [MVP]

Alan said:
Hello:

Is it possible to delete the last character from the text file?

Thanks!

Yes! It is possible! See the post by Matthias Tacke in the "Trim
Last Line" thread. The following is a slight modification to remove
the last character.

::StripLastChar.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal enabledelayedexpansion
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
) else (
set rec=%%B
echo !rec:~0,-1!
)
)
::StripLastChar.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
M

Matthias Tacke

Phil Robyn said:
Yes! It is possible! See the post by Matthias Tacke in the "Trim
Last Line" thread. The following is a slight modification to remove
the last character.

::StripLastChar.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
<snip>

Hello Phil,
I'm a bit curious, I always got rec:~0,-1 in the output.

The echo status on empty lines is also present. This worked (but not
with html files where the last char is a closing bracket ;-)

::StripLastChar.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
) else (
set "rec=%%B"
call echo.%%rec:~0,-1%%
)
)
::StripLastChar.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
P

Phil Robyn [MVP]

Matthias said:
:



<snip>

Hello Phil,
I'm a bit curious, I always got rec:~0,-1 in the output.

Hi, Matthias,

Well, unlike my usual practice, I didn't actually *test* the suggested
code; I merely copied your example and made what I thought would be the
appropriate change. :)
The echo status on empty lines is also present. This worked (but not
with html files where the last char is a closing bracket ;-)

The original poster's "the text file" was not specific enough. Of course,
depending on what is actually in a particular file, one may have to make
certain modifications (for example, to write out blank lines, etc.) And
html files where the last character is a closing bracket might also pose
a problem. But in general, it is indeed *possible* to remove the last
character from a text file via a batch file; it's just that a batch file
might not be the most *practical* means of doing so, depending on what is
in the text file in question.
::StripLastChar.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
) else (
set "rec=%%B"
call echo.%%rec:~0,-1%%
)
)
::StripLastChar.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
M

Matthias Tacke

Phil Robyn said:
Hi, Matthias,

Well, unlike my usual practice, I didn't actually *test* the suggested
code; I merely copied your example and made what I thought would be the
appropriate change. :)
No critic Phil, just a correction for my own mistake. The way with the
numered find I'd choosen to handle empty lines and then forgot that damn
echo status.

Have a nice sunday.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top