@echo off setlocal call :fileAttributes %1 && ( set fSize set lnCnt set lnLen set EOLSize set finalEOL set EOFSize ) exit /b :FileAttributes File :: :: Determines attributes of a text file with fixed line length :: :: Lines can be terminated with or :: :: This script can only handle lines up to length 8191 :: :: returns :: fSize = file size in bytes :: lnCnt = line count :: lnLen = line length in bytes :: EOLSize = number of characters used to signify EOL :: finalEOL = 0 if last line is missing EOL :: 1 if last line has EOL :: undefined if not able to determine :: EOFSize = 0 if file is NOT terminated by EOF :: 1 if file IS terminated by EOF :: undefined if not able to determine :: :: Sets ERRORLEVEL > 0 if error :: 1 File not found :: 2 First line is empty :: 3 Too few lines (need at least 2) :: 4 Line length too small (must be at least 4) :: 5 Invalid file format :: setlocal disableDelayedExpansion set "fsize=" for %%F in ("%~1") do set fsize=%%~zF if not defined fsize >&2 echo ERROR: File "%~1" not found&exit /b 1 set tmpFile="%temp%\FileAttributes%random%.tmp" >%tmpFile% findstr /o "^" "%~1" set "ln1=" set "ln2=" for /f "usebackq delims=" %%L in (%tmpFile%) do ( if not defined ln1 (set "ln1=%%L") else set "ln2=%%L"&goto :break ) :break del %tmpfile% setlocal enableDelayedExpansion set "ln1=!ln1:*:=!" if not defined ln1 >&2 echo ERROR: First line is empty&exit /b 2 if not defined ln2 >&2 echo ERROR: Too few lines (need at least 2)&exit /b 3 call :strLen ln1 lnLen if %lnLen% lss 4 >&2 echo ERROR: Line length to small (must at least 4)&exit /b 4 for /f "delims=:" %%A in ("!ln2!") do set /a "EOLSize=%%A-lnLen" set /a "lnLen2=lnLen+EOLSize, lnCnt=fSize/lnLen2, fSize2=lnCnt*lnLen2, fSize3=((lnCnt+1)*lnLen2)-EOLSize, fSize2a=fSize2+1, fSize3a=fSize3+1" if %fSize2%==%fSize% ( if %EOLSize%==2 ( set finalEOL=1 set EOFSize=0 ) else ( set "finalEOL=" set "EOFSize=" ) ) else if %fSize2a%==%fSize% ( set finalEOL=1 set EOFSize=1 ) else if %fSize3%==%fSize% ( set /a "lnCnt+=1" set finalEOL=0 set EOFSize=0 ) else if %fSize3a%==%fSize% ( set /a "lnCnt+=1" set finalEOL=0 set EOFSize=1 ) else ( >&2 echo ERROR: Invalid file format&exit /b 5 ) endlocal & endlocal & ( set "fSize=%fsize%" set "lnLen=%lnLen%" set "lnCnt=%lnCnt%" set "EOLSize=%EOLSize%" set "finalEOL=%finalEOL%" set "EOFSize=%EOFSize%" ) exit /b 0 :strLen string len -- returns the length of a string :: -- string [in] - variable name containing the string being measured for length :: -- len [out] - variable to be used to return the string length :: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient :$created 20081122 :$changed 20101116 :$categories StringOperation :$source http://www.dostips.com ( SETLOCAL ENABLEDELAYEDEXPANSION set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound rem it also avoids trouble in case of empty string set "len=0" for /L %%A in (12,-1,0) do ( set /a "len|=1<<%%A" for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A" ) ) ( ENDLOCAL & REM RETURN VALUES IF "%~2" NEQ "" SET /a %~2=%len% ) EXIT /b