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

test last character in file

24 views
Skip to first unread message

charles

unread,
May 31, 2008, 10:09:56 AM5/31/08
to
I have a log file that may or may not end with a carriage return. How
can I test what the last character of the file is and insert a <CRLF>
if needed?

(If the last char is not a ^M then add ^M.)

Thanks for any help.


Timo Salmi

unread,
May 31, 2008, 10:41:28 AM5/31/08
to
charles <som...@somewhere.invalid> wrote:
> I have a log file that may or may not end with a carriage return. How
> can I test what the last character of the file is and insert a <CRLF>
> if needed?

Why bother with the testing? Just always type (with for) the file
redirecting it to another file.

@echo off
if exist whatever2 del whatever2
for /f "tokens=*" %%a in (whatever) do echo %%a >>whatever2
if exist whatever if exist whatever2 del whatever
ren whatever2 whatever

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FI-65101, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

billious

unread,
May 31, 2008, 11:07:05 AM5/31/08
to

"charles" <som...@somewhere.invalid> wrote in message
news:6ad4hkF...@mid.individual.net...

more filename>newfilename

should append ^J if missing from last line


Ted Davis

unread,
May 31, 2008, 11:17:12 AM5/31/08
to

I wouldn't bother with a test - I'd just

echo.>> logfile

If it were absolutely essential to avoid an extra blank line at the end,
I'd:

awk "{print $0}" logfile > newfile

where awk is gawk.exe from
<http://gnuwin32.sourceforge.net/packages/gawk.htm>

If were essential to avoid creating an intermediate file, I'd:

awk "{A[NR]=$0}END{y=FILENAME;close(FILENAME);for(x=1;x<=NR;x++)print
A[x]>y}"

Not tested - typos are likely.

--

T.E.D. (tda...@mst.edu) MST (Missouri University of Science and Technology)
used to be UMR (University of Missouri - Rolla).

charles

unread,
Jun 1, 2008, 8:47:20 AM6/1/08
to
On Sat, 31 May 2008 14:09:56 +0000, charles
<som...@somewhere.invalid> wrote:

Thank you all. Not only did all your samples work but you gave me a
better way to think about the problem. Much appreciated!

Todd Vargo

unread,
Jun 2, 2008, 11:19:26 PM6/2/08
to

This tests the last character and adds a CRLF only if the last character is
not a LF.

@echo off
echo> $.vbs filespec = "test.txt"
echo>>$.vbs Set fso = CreateObject("Scripting.FileSystemObject")
echo>>$.vbs fs = fso.GetFile(filespec).Size
echo>>$.vbs Set f = fso.OpenTextFile(filespec,1)
echo>>$.vbs f.Skip(fs-1)
echo>>$.vbs If Not f.Read(1) = Chr(10) Then
echo>>$.vbs Set f = fso.OpenTextFile(filespec,8)
echo>>$.vbs f.Write vbCRLF
echo>>$.vbs End If
cscript /nologo $.vbs
del $.vbs

--
Todd Vargo (remove hyphen to reply by email)
Never trust anyone who polices newsgroups hypocritically or supports
knowingly providing misinformation.


charles

unread,
Jun 5, 2008, 8:37:17 AM6/5/08
to

Thanks.

0 new messages