163} How do I test and ensure that <CR><LF> is the last character pair of my log file?
"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
<CR><LF> if needed?"
Why bother with the testing? Just always type (with for) the file
redirecting it to another file. For example
@echo off & setlocal enableextensions
if exist "%temp%\templog.lis" del "%temp%\templog.lis"
for /f "tokens=*" %%a in ('type "C:\_M\My log file.txt"') do (
echo %%a >>"%temp%\templog.lis")
if exist "MyLogFile.txt" if exist "%temp%\templog.lis" del "MyLogFile.txt"
move /-y "%temp%\templog.lis" "MyLogFile.txt"
endlocal & goto :EOF
or
@echo off & setlocal enableextensions
set mylog_=C:\_M\My log file.txt
if exist "%temp%\templog.lis" del "%temp%\templog.lis"
more "%mylog_%">"%temp%\templog.lis"
if exist "%mylog_%" if exist "%temp%\templog.lis" del "%mylog_%"
move /-y "%temp%\templog.lis" "%mylog_%"
endlocal & goto :EOF
or
@echo off & setlocal enableextensions
set mylog_=C:\_M\My log file.txt
if exist "%temp%\templog.lis" del "%temp%\templog.lis"
find /v "" <"%mylog_%" >"%temp%\templog.lis"
if exist "%mylog_%" if exist "%temp%\templog.lis" del "%mylog_%"
move /-y "%temp%\templog.lis" "%mylog_%"
endlocal & goto :EOF
Also see the somewhat similar item
#139.
There are subtle differences between
TYPE
and
MORE. Consider the following two text
files (where this first one's last line is missing the
customary
<CR><LF> pair):
@echo off & setlocal enableextensions
for %%f in ("file*.txt") do
type "%%f
"
endlocal & goto :EOF
The output will be:
C:\TEST>CMDFAQ.CMD
First line of file one
Last line of file oneFirst line of file two
@echo off & setlocal enableextensions
for %%f in ("file*.txt") do
more "%%f
"
endlocal & goto :EOF
The output will be:
C:\TEST>CMDFAQ.CMD
First line of file one
Last line of file one
First line of file two
Any of the above solves the problem, one way or another, but what if
one anyway is interested in getting the last character of a file? This
can be done e.g. with a Visual Basic Script (VBScript) aided command
line script:
@echo off & setlocal enableextensions
::
set myfile_=C:\_M\My log file.txt
if not exist "%myfile_%" (
echo File "%myfile_%" not found
goto :EOF)
::
:: Build a Visual Basic Script and run it
set vbs_="%temp%\tmp$$$.vbs"
set skip=
findstr "'%skip%VBS" "%~f0" > %vbs_%
for /f %%a in ('cscript //nologo %vbs_%') do (
set lastcharcode=%%a)
echo The lastcharcode for "%myfile_%" is %lastcharcode%
::
:: Clean up
for %%f in (%vbs_%) do if exist %%f del %%f
::
endlocal & goto :EOF
'
'The Visual Basic Script
Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
Dim fso, f, myfile, filesize 'VBS
Set WshShell = WScript.CreateObject("WScript.Shell") 'VBS
myfile = WshShell.ExpandEnvironmentStrings("%myfile_%") 'VBS
Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
filesize = fso.GetFile(myfile).Size 'VBS
Set f = fso.OpenTextFile(myfile, ForReading) 'VBS
f.Skip(filesize-1) 'VBS
WScript.Echo Asc(f.Read(1)) 'VBS
The output could be e.g.
C:\_D\TEST>cmdfaq
The lastcharcode for "C:\_M\My log file.txt" is 10