Check MAXIMUM RECORD LENGTH

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Check MAXIMUM RECORD LENGTH

#1 Post by darioit » 15 Sep 2010 04:37

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check MAXIMUM RECORD LENGTH

#2 Post by aGerman » 15 Sep 2010 07:14

Would you please give an example content of such a file.

Regards
aGerman

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check MAXIMUM RECORD LENGTH

#3 Post by darioit » 15 Sep 2010 07:27

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check MAXIMUM RECORD LENGTH

#4 Post by aGerman » 15 Sep 2010 08:05

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check MAXIMUM RECORD LENGTH

#5 Post by darioit » 15 Sep 2010 08:09

GREAT!! Many thanks!!
Now i'd like to analyze how it's work :P

Regards
Dario

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check MAXIMUM RECORD LENGTH

#6 Post by aGerman » 15 Sep 2010 08:25

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

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Check MAXIMUM RECORD LENGTH

#7 Post by darioit » 15 Sep 2010 08:39

yes understand, but why we can't use this code

Code: Select all

findstr /o $ %record%

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Check MAXIMUM RECORD LENGTH

#8 Post by ghostmachine4 » 15 Sep 2010 10:14

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.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Check MAXIMUM RECORD LENGTH

#9 Post by !k » 15 Sep 2010 12:04

Code: Select all

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check MAXIMUM RECORD LENGTH

#10 Post by aGerman » 15 Sep 2010 12:51

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Check MAXIMUM RECORD LENGTH

#11 Post by jeb » 15 Sep 2010 15:00

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, :)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check MAXIMUM RECORD LENGTH

#12 Post by aGerman » 15 Sep 2010 15:53

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Check MAXIMUM RECORD LENGTH

#13 Post by ghostmachine4 » 15 Sep 2010 20:06

!k wrote:

Code: Select all

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

that's another way.
Last edited by ghostmachine4 on 15 Sep 2010 20:19, edited 1 time in total.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Check MAXIMUM RECORD LENGTH

#14 Post by ghostmachine4 » 15 Sep 2010 20:14

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.

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Check MAXIMUM RECORD LENGTH

#15 Post by amel27 » 15 Sep 2010 23:28

Code: Select all

@echo off

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

Post Reply