Page 1 of 2

Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 04:37
by darioit
Hello folks,

how can I check MAXIMUM RECORD LENGTH in whole file that cannot be greater the a value

Example: Max Rec Length = 100 byte
read all record in a file
If only 1 record is > than 100 echo error record length

Thanks in advance
Dario

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 07:14
by aGerman
Would you please give an example content of such a file.

Regards
aGerman

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 07:27
by darioit
the max record lenght of this file must be 80, but the 2 row is 81 and this is not good

File1.txt

Code: Select all

CODE1QWERTYYUIOPASDFGHJKLZXCCVVBNM                                            0 
CODE2QWERTYYUIOPASDFGHJKLZ1234565MX                                            0
CODE3QWERTYYUIOPASDFGHJKLZX987654M                                            0


I think something like
checkfile.bat file1.txt 80 (where 80 is max record length)

Regards
Dario

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 08:05
by aGerman
Line 2 has 80 characters, but doesn't matter :wink:

checkfile.bat

Code: Select all

@echo off &setlocal
for /f "tokens=1* delims=:" %%a in ('findstr /n . "%~1"') do (
  set "lineNo=%%a"
  set "record=%%b"
  call :proc %~2
)
pause
goto :eof

:proc
for /f "delims=:" %%i in ('^(echo\"%record%"^&echo\^)^|findstr /o $') do set /a length=%%i-5
if %length% gtr %1 echo Line %lineNo%: %length% characters.
goto :eof


Call it like you said: call checkfile.bat file1.txt 80

Regards
aGerman

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 08:09
by darioit
GREAT!! Many thanks!!
Now i'd like to analyze how it's work :P

Regards
Dario

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 08:25
by aGerman
Have a look at findstr /?
Option /o returns the offset.

Try

Code: Select all

@echo off
(echo\"ABC"&echo\)|findstr /o $
pause

You need to subtract 5 for quotes and line break. Result: 3 for ABC.

Regards
aGerman

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 08:39
by darioit
yes understand, but why we can't use this code

Code: Select all

findstr /o $ %record%

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 10:14
by ghostmachine4
download and use a good text processing tool like gawk for windows

Code: Select all

c:\test> gawk.exe  "length>80{print \"bad record: \"$0\" at line \"NR}" file


advantages:
1) shorter than batch code and does the job
2) runs much faster than batch especially when you have big files to process.

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 12:04
by !k

Code: Select all

sed.exe -n /^.\{81\}/= file.txt
where
81 = MaxRecLength + 1

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 12:51
by aGerman
darioit wrote:yes understand, but why we can't use this code

Code: Select all

findstr /o $ %record%

1. In this case FINDSTR would look for a file named like the value of %record%. This is the reason why you have to pipe the ECHO output to FINDSTRING.
2. /o returns the offset for the line before, thats why I added an empty line. In the first line the offset is always 0.

Regards
aGerman

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 15:00
by jeb
Ok the solutions of !k and ghostmachine are good, but it's not batch.
And this is the batch forum not the "which tool is better than cmd.exe"

So there is the solution of aGerman, but ...
it fails :cry:

I only append one test line

Code: Select all

CODE1QWERTYYUIOPASDFGHJKLZXCCVVBNM                                             0
CODE2QWERTYYUIOPASDFGHJKLZ1234565MX                                            012
CODE3QWERTYYUIOPASDFGHJKLZX987654M                                             0
"ODE2QWERTYYUIOPASDFGHJKLZ1234565MX                                            012


But perhaps there is a more stable solution, :)

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 15:53
by aGerman
jeb wrote:Ok the solutions of !k and ghostmachine are good, but it's not batch.
And this is the batch forum not the "which tool is better than cmd.exe"

On the right hand I agree with you, but on the left hand these solutions are batch as well (but no native batch).

jeb wrote:So there is the solution of aGerman, but ...
it fails :cry:

I only append one test line

Code: Select all

CODE1QWERTYYUIOPASDFGHJKLZXCCVVBNM                                             0
CODE2QWERTYYUIOPASDFGHJKLZ1234565MX                                            012
CODE3QWERTYYUIOPASDFGHJKLZX987654M                                             0
"ODE2QWERTYYUIOPASDFGHJKLZ1234565MX                                            012


But perhaps there is a more stable solution, :)


I knew before that it would fail if there is an uneven number of double quotes in a line, but I was virtually certain that it would work for darioit's file.

Similar to !k's sed solution we could use the marginal RegEx capability of FINDSTR.

Code: Select all

@echo off &setlocal
setlocal EnableDelayedExpansion
for /l %%i in (0,1,%~2) do set "dots=!dots!."
echo Lines with more than %~2 characters:
for /f "delims=:" %%i in ('findstr /n "%dots%" "%~1"') do (set "x=1" &echo %%i)
if not defined x echo No line.
pause


Regards
aGerman

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 20:06
by ghostmachine4
!k wrote:

Code: Select all

sed.exe -n /^.\{81\}/= file.txt
where
81 = MaxRecLength + 1

that's another way.

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 20:14
by ghostmachine4
jeb wrote:Ok the solutions of !k and ghostmachine are good, but it's not batch.
And this is the batch forum not the "which tool is better than cmd.exe"

nonsense. findstr is also not batch. Its an external command as well.
the OP did not ask "which tool is better than cmd.exe to solve this ..". so don't imply that yourself.
Its true you can somehow do this in pure batch(cmd.exe) + windows (already installed) tools such as findstr....but its also true
that there are other ways to solve this with less hassle, and faster performance... by using a tool that is suitable for the job.


But perhaps there is a more stable solution, :)

sure there is. 2 of them already provided.

Re: Check MAXIMUM RECORD LENGTH

Posted: 15 Sep 2010 23:28
by amel27

Code: Select all

@echo off

for /f "tokens=1 delims=:" %%i in (
'findstr /bnrc:"................................................................................." "%~1"'
) do echo %%i