Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete Last Line of TEXT file using a BAT

8,025 views
Skip to first unread message

Brett

unread,
Jan 15, 2010, 7:53:34 AM1/15/10
to
Hello,

Please can you let me know how I can delete the last line of a TXT
file. For example my file is called "fileA.txt" and I will like to
run a batch to delete the list line in "fileA.txt". i.e. FileA is
currently:

1,NAME

I would just like to display

1,NAME

removing the carriage return.

Thanks,
Brett

Ted Davis

unread,
Jan 15, 2010, 8:41:49 AM1/15/10
to

Saying you want to do it in a batch file rather than saying you want to
do it in batch language leaves an opening for a simple solution:

head --lines=-1 infile > outfile

where head is from the GnuWin32 (free and open source) CoreUtils package:
http://gnuwin32.sourceforge.net/packages/coreutils.htm


--
T.E.D. (tda...@mst.edu)

Brett

unread,
Jan 15, 2010, 8:52:29 AM1/15/10
to

I fixed the issue, the problem was with the copy command I was using.
I am now using copy /b *.TXT NewFile.TXT. Now there is no
carriagereturns.

Thanks,

Brett

Badrune

unread,
Feb 16, 2010, 5:54:44 AM2/16/10
to

I've exactply the same problem : how can i use FIND to filter out these
lines? "FIND" or "FINDSTR" can find these lines, but when i use ">" to
output a file, the output itself contains that char!

:-(((


foxidrive

unread,
Feb 16, 2010, 7:48:10 AM2/16/10
to

Are you using something like this?

find "1,NAME" <"filein.txt" >"fileout.txt"

--
Regards,
Mic

Badrune

unread,
Feb 18, 2010, 7:27:33 AM2/18/10
to

yes :-/


foxidrive

unread,
Feb 18, 2010, 8:36:09 AM2/18/10
to
On Thu, 18 Feb 2010 13:27:33 +0100, "Badrune"
<raoulbova...@pixiedixi.invalid> wrote:

>>> I've exactply the same problem : how can i use FIND to filter out
>>> these lines? "FIND" or "FINDSTR" can find these lines, but when i
>>> use ">" to output a file, the output itself contains that char!
>>>
>>> :-(((
>>
>> Are you using something like this?
>>
>> find "1,NAME" <"filein.txt" >"fileout.txt"
>
>yes :-/
>

Please explain your problem and show us what is in the file before and
afterwards.


--
Regards,
Mic

billious

unread,
Feb 18, 2010, 8:46:35 AM2/18/10
to

"Badrune" <raoulbova...@pixiedixi.invalid> wrote in message
news:4b7d3248$0$1132$4faf...@reader1.news.tin.it...

I suspect that OP's problem was that copy was appending a ^Z, not a
"carriage return" as claimed. This would be cured by the /b switch.

So what is "that char" that you don't want in the output? Do you actually
want a Unix-style 0AH new-line rather than 0D0AH? If so, I'd use a SED port,
and look for Eric Pement's SED one-liners.

>


Badrune

unread,
Feb 19, 2010, 3:30:04 AM2/19/10
to

sorry if my english isn't too good...

in the DOS times i were able to tell you what this char could be... but now
i've lost this skill.
I have a file that comes out by an export function of a program, and in a
phase of my workflow another program need to import it ... but it in some
parsing moment it doesn't pass the check: if I open the file with a simple
notepad, erase the "square" char in the last line resulting in the last
data-line is the real last line, all goes well.

but if i try to use FIND or FINDSTR with input: file_with_wrong_char and
>output_file , i have another file with a wrong line.

Badrune

unread,
Feb 19, 2010, 3:36:04 AM2/19/10
to


see the problem in response of "bilious" post;
but sorry: i am not able to tell you exactly the content of the file... some
of the informations are strictly reserved (in each correct line) ... last
line has a wrong char.
maybe i can use the gnu program as suggested by Tedd Davis... because infact
it's
the LAST line ... i hope well ... don't know if the program can run without
excessive requirements (dll, libraries...) ... because at the end of the
work i have to leave the result to a user workstation without administrative
rights...

thanks for any help you can give me , folks!

foxidrive

unread,
Feb 19, 2010, 4:07:06 AM2/19/10
to
On Fri, 19 Feb 2010 09:36:04 +0100, "Badrune"
<raoulbova...@pixiedixi.invalid> wrote:

>see the problem in response of "bilious" post;
>but sorry: i am not able to tell you exactly the content of the file... some
>of the informations are strictly reserved (in each correct line) ... last
>line has a wrong char.
>maybe i can use the gnu program as suggested by Tedd Davis... because infact
>it's
>the LAST line ... i hope well ... don't know if the program can run without
>excessive requirements (dll, libraries...) ... because at the end of the
>work i have to leave the result to a user workstation without administrative
>rights...
>
>thanks for any help you can give me , folks!

If the file has something you can filter on then this works here. I
filtered on the character "a"

find "a" <file4.txt >file6.txt

--
Regards,
Mic

Badrune

unread,
Feb 19, 2010, 9:30:36 AM2/19/10
to

if i use the filter as is, it works, if i use it outputting in another file,
this creates the same character i try to filter with it


billious

unread,
Feb 19, 2010, 10:09:28 AM2/19/10
to

"Badrune" <raoulbova...@pixiedixi.invalid> wrote in message
news:4b7e4d76$0$1139$4faf...@reader1.news.tin.it...
> billious wrote:
[snip]

>>>>>
>>>>> I've exactply the same problem : how can i use FIND to filter out
>>>>> these lines? "FIND" or "FINDSTR" can find these lines, but when i
>>>>> use ">" to output a file, the output itself contains that char!
>>>>>
>>>>> :-(((
>>>>
>>>> Are you using something like this?
>>>>
>>>> find "1,NAME" <"filein.txt" >"fileout.txt"
>>>
>>> yes :-/
>>
>> I suspect that OP's problem was that copy was appending a ^Z, not a
>> "carriage return" as claimed. This would be cured by the /b switch.
>>
>> So what is "that char" that you don't want in the output? Do you
>> actually want a Unix-style 0AH new-line rather than 0D0AH? If so, I'd
>> use a SED port, and look for Eric Pement's SED one-liners.
>
> sorry if my english isn't too good...
>
> in the DOS times i were able to tell you what this char could be... but
> now i've lost this skill.
> I have a file that comes out by an export function of a program, and in a
> phase of my workflow another program need to import it ... but it in some
> parsing moment it doesn't pass the check: if I open the file with a simple
> notepad, erase the "square" char in the last line resulting in the last
> data-line is the real last line, all goes well.
>
> but if i try to use FIND or FINDSTR with input: file_with_wrong_char and
> >output_file , i have another file with a wrong line.
>

I suspect that if FIND or FINDSTR is producing an output that still contains
the unwanted character(s) then the actual format of the input file is

string_containing_wanted_data[CR]unwanted_character(s)

Without knowing the exact format or the actual (hex-value of) unwanted
characters, this could be difficult.

I'd try

for /f "delims=" %%i in ( ' find "wanted" ^<filename ' ) do set var=%%i
>outfilename echo=%var:~0,-2%

(two lines)

Where "-2" removes the last 2 characters - may need to be changed to suit
the circumstance, depending on the length of the unwanted sequence.


0 new messages