Hi
I need to count the number of records in a file that starts with the value 'A01'. If this value is found in other parts of the string, it should be ignored.
Can someone help please?
Count the no of records in a file using positional search string
Moderator: DosItHelp
-
- Posts: 3
- Joined: 11 Aug 2017 03:42
-
- Posts: 21
- Joined: 04 Aug 2017 14:20
- Location: France
Re: Count the no of records in a file using positional search string
Hi, try this :
Code: Select all
@echo off
set /a number=0
for /f "usebackq delims=" %%A in ("file.txt") do (echo %%A | findstr /b "A01" >nul && set /a number+=1)
echo number of records = %number%
pause
exit
-
- Posts: 3
- Joined: 11 Aug 2017 03:42
Re: Count the no of records in a file using positional search string
Ugh - That is a horribly inefficient way to do it. You might not notice with a small file. But a large file will be painfully slow.
If all you want is to print the count to the screen, then
If you want to capture the value in a variable, then
Dave Benham
If all you want is to print the count to the screen, then
Code: Select all
findstr /b "A01" "file.txt" | find /n /v ""
If you want to capture the value in a variable, then
Code: Select all
for /f %%N in ('findstr /b "A01" "file.txt" ^| find /n /v ""') do set count=%%N
Dave Benham
-
- Posts: 21
- Joined: 04 Aug 2017 14:20
- Location: France
Re: Count the no of records in a file using positional search string
Thanks for the correction, I still have a few techniques to learn
-
- Posts: 3
- Joined: 11 Aug 2017 03:42
Re: Count the no of records in a file using positional search string
thank you for you help dbenham