PC Review


Reply
Thread Tools Rate Thread

How can I delete the last character of the text file

 
 
Alan Tang
Guest
Posts: n/a
 
      12th Jun 2004
Hello:

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

Thanks!

 
Reply With Quote
 
 
 
 
Phil Robyn [MVP]
Guest
Posts: n/a
 
      12th Jun 2004
Alan Tang wrote:
> 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::::::::::::::::::::::::::::::::::::::::::::::::::::::

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l
 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      12th Jun 2004
"Phil Robyn [MVP]" wrote:

>Alan Tang wrote:
>> 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::::::::::::::::::::::::::::::::::::::::::::::::::::::

<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::::::::::::::::::::::::::::::::::::::::::::::::::::::


--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
Reply With Quote
 
Phil Robyn [MVP]
Guest
Posts: n/a
 
      13th Jun 2004
Matthias Tacke wrote:
> "Phil Robyn [MVP]" wrote:
>
>
>>Alan Tang wrote:
>>
>>>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::::::::::::::::::::::::::::::::::::::::::::::::::::::

>
> <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::::::::::::::::::::::::::::::::::::::::::::::::::::::
>
>



--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l
 
Reply With Quote
 
Matthias Tacke
Guest
Posts: n/a
 
      13th Jun 2004
"Phil Robyn [MVP]" wrote:
>Matthias Tacke wrote:
>> "Phil Robyn [MVP]" wrote:

<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. :-)
>

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.

--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting Text File - Append Sequence Character on File Name krista.m.chase@gmail.com Microsoft Access 5 7th Jan 2008 07:18 PM
Embed a hex character in a text file Shane Microsoft VB .NET 1 12th Jan 2007 06:28 AM
importing undelimited text file data, character-by-character =?Utf-8?B?VGhlIE1vcyQ=?= Microsoft Excel Programming 4 26th Dec 2005 11:01 AM
Delete one character from a text field =?Utf-8?B?RGF2ZSBDb2x1bWJ1cw==?= Microsoft Access Queries 2 8th Nov 2005 07:00 PM
delete a character from a file =?Utf-8?B?RW1tYW5vdWVs?= Microsoft VC .NET 2 6th Jun 2004 10:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:49 PM.