Check MAXIMUM RECORD LENGTH
Moderator: DosItHelp
Check MAXIMUM RECORD LENGTH
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
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
Would you please give an example content of such a file.
Regards
aGerman
Regards
aGerman
Re: Check MAXIMUM RECORD LENGTH
the max record lenght of this file must be 80, but the 2 row is 81 and this is not good
File1.txt
I think something like
checkfile.bat file1.txt 80 (where 80 is max record length)
Regards
Dario
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
Line 2 has 80 characters, but doesn't matter
checkfile.bat
Call it like you said: call checkfile.bat file1.txt 80
Regards
aGerman
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
GREAT!! Many thanks!!
Now i'd like to analyze how it's work
Regards
Dario
Now i'd like to analyze how it's work
Regards
Dario
Re: Check MAXIMUM RECORD LENGTH
Have a look at findstr /?
Option /o returns the offset.
Try
You need to subtract 5 for quotes and line break. Result: 3 for ABC.
Regards
aGerman
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
yes understand, but why we can't use this code
Code: Select all
findstr /o $ %record%
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Check MAXIMUM RECORD LENGTH
download and use a good text processing tool like gawk for windows
advantages:
1) shorter than batch code and does the job
2) runs much faster than batch especially when you have big files to process.
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
Code: Select all
sed.exe -n /^.\{81\}/= file.txt
81 = MaxRecLength + 1
Re: Check MAXIMUM RECORD LENGTH
darioit wrote:yes understand, but why we can't use this codeCode: 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
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
I only append one test line
But perhaps there is a more stable solution,
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
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
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
I only append one test lineCode: 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
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Check MAXIMUM RECORD LENGTH
!k wrote:whereCode: Select all
sed.exe -n /^.\{81\}/= file.txt
81 = MaxRecLength + 1
that's another way.
Last edited by ghostmachine4 on 15 Sep 2010 20:19, edited 1 time in total.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Check MAXIMUM RECORD LENGTH
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
Code: Select all
@echo off
for /f "tokens=1 delims=:" %%i in (
'findstr /bnrc:"................................................................................." "%~1"'
) do echo %%i