Reputation: 2393
I'm having a text file and I want to run a batch file which reads the entire text file and tells me the no: of occurrences of a string "PASS":
Could you pl help how to do that - I'm not able to understand the usage of tokens in BAT file.
Upvotes: 2
Views: 8516
Reputation: 10714
Maybe the findstr
command will help you: Findstr Help.
It doesn't print the number of occurences, but maybe you can do something with the results.
UPDATE:
The Find command has a /c
option, which counts the number of lines containing that string.
Upvotes: 4
Reputation: 82307
The answer of Mulmoth is good and normally it should solve your problem.
But as you don't understand the tokens, I will try to explain it.
You can read the content of a file with the FOR/F
command.
The FOR/F
will read a file line by line, each empty line will be skip, also every line beginning with the EOL-character (default is ;
).
It uses per default tokens=1 delims=<TAB><SPACE> EOL=;
.
In this case you got always one token till the line end or till the first SPACE
or TAB
.
for /F "tokens=1" %%a in (myFile.txt) do (
echo token1=%%a
)
if you want to read more tokens then you need to define it.
for /F "tokens=1,2,3,4" %%a in (myFile.txt) do (
echo token1=%%a token2=%%b token3=%%c token4=%%d
)
Now the line will split into four tokens at the delimiter (SPACE or TAB) if there are less delims in a line then the tokens are empty.
If you don't want multiple tokens, or after a specific token the rest of the line you can use the *
.
for /F "tokens=1,2,3,4*" %%a in (myFile.txt) do (
echo token1=%%a token2=%%b token3=%%c token4=%%d
)
Now, in token4 will be the fourth token and the rest of the line.
for /F "tokens=1*" %%a in (myFile.txt) do (
echo token1=%%a token2=%%b token3=%%c token4=%%d
)
In this case only one token exists (%%a
) the others (%%b
%%c
%%d
) aren't valid tokens and the text token2=%b token3=%c token4=%d
will be echoed.
You could also use the delim to reduce all to only one token. for /F "delims=" %%a in (myFile.txt) do ( echo complete line=%%a )
This works, as there can only one token, as there isn't any character left to split the line into more tokens.
Upvotes: 3