Windows command processor cmd.exe
executing batch files line by line is definitely not the right application for this task. It is designed to execute commands and applications. However, here is a batch file counting parentheses/round brackets in a text file.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OpenBracketCount=0"
set "CloseBracketCount=0"
for /F "usebackq tokens=* eol=" %%I in ("File.txt") do (
set "TextLine=%%I"
call :ProcessLine
)
echo Number of opening brackets: %OpenBracketCount%
echo Number of closing brackets: %CloseBracketCount%
endlocal
goto :EOF
:ProcessLine
set "TextLine=%TextLine:"=%"
if not defined TextLine goto :EOF
set "TempLine=%TextLine:(=%"
if not defined TempLine goto BracketCount
set "TempLine=%TempLine:)=%"
if "%TempLine%" == "%TextLine%" goto :EOF
:BracketCount
if "%TextLine:~0,1%" == "(" ( set /A "OpenBracketCount+=1" ) else if "%TextLine:~0,1%" == ")" set /A CloseBracketCount+=1
set "TextLine=%TextLine:~1%"
if defined TextLine goto BracketCount
goto :EOF
Command FOR with using option /F
to process lines from a text file ignores by default empty lines and lines starting with a semicolon which is the default for end of line option eol
, and splits up the line into substrings (tokens) on spaces tabs. It is okay to ignore empty lines, but lines with a ;
at beginning should not be ignored for counting parentheses.
Both unwanted line processing behaviors could be disabled with using usebackq^ delims^=^ eol^=
to get the entire line assigned to the environment variable TextLine
by specifying no delimiters and no end of line character. It is not possible to enclose this option string in double quotes in this special case which requires to escape the spaces and equal signs interpreted by default as argument string separators by caret character ^
to interpret them as literal characters.
But better is to use double quoted option string "usebackq tokens=* eol="
which defines also no end of line character, but keeps space and horizontal tab as delimiters. So assigned to loop variable I
is the line after removing leading spaces/tabs. That is good here as it avoids processing lines containing only spaces/tabs and can reduce also the number of characters to process in the subroutine.
In the subroutine ProcessLine
first all double quotes are removed from the line read from text file as a "
could later in remaining command lines result in a syntax error on execution. It is of course possible that a line contains only one or more "
resulting in environment variable TextLine
is not defined anymore after removing all double quotes in which case the line definitely does not contain parentheses to count.
Next is assigned to environment variable TempLine
the line read from text file (without leading spaces/tabs and without all double quotes) with all (
and )
removed from the line. The line does not contain a parenthesis if TempLine
is equal TextLine
and so the subroutine can be exited immediately to reduce the execution time of the batch file.
Otherwise the current line contains at least one round bracket and so it is necessary to compare each character of the remaining line with (
and )
to count them .
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
set /?
setlocal /?
See also Where does GOTO :EOF return to?