Tonight I was looking at a batch script method of checking to see if the same computer name appears in multiple (2) text files. If so I'll use an IF statement to do other things.
I figured I could use the FIND command's /C switch and multiple text file search with a FOR loop to get a count of the number of instances that search term appears. I also wanted to add the FINDSTR command's /L command to make that search term literal/exact (so that abc1234 or 2abc123 in one of the text files didn't return a false positive if the computer name being searched for is abc123).
I came up with the following code, which works, but I am wondering if there is a way that this code could be optimized further (reducing the number of lines).
I'm also curious if there is a way to use both a FINDSTR and FIND command within a FOR loop with a pipe but pass the search results from the FINDSTR to the FIND command in order to get the benefits of the /L switch the FINDSTR command utilizes with the /C swithc FIND utilizes?
For example FOR /F "TOKENS=* DELIMS=" %%a IN ('FINDSTR /I /X %COMPUTERNAME% mathtestok.txt ^| FIND /C "COMPUTERNAME" mathtestok.txt') DO ECHO %%a works, but is essentially running the FINDSTR and FIND commands parallel to one another, not in tandem.
Here's the code I wrote:
FOR /F "TOKENS=* DELIMS=" %%a IN ('FINDSTR /I /X %COMPUTERNAME% mathtestok.txt') DO SET FINDSTR1=%%a
FOR /F "TOKENS=3 DELIMS=: " %%b IN ('FIND /C "%FINDSTR1%" mathtestok.txt') DO SET RESULTS1=%%b
FOR /F "TOKENS=* DELIMS=" %%a IN ('FINDSTR /I /X %COMPUTERNAME% readingtestok.txt') DO SET FINDSTR2=%%a
FOR /F "TOKENS=3 DELIMS=: " %%b IN ('FIND /C "%FINDSTR2%" readingtestok.txt') DO SET RESULTS2=%%b
SET /A COUNT=%RESULTS1%+%RESULTS2%
IF %COUNT% GTR 1 ECHO COUNT=%COUNT% (Is Greater Than 1)
IF %COUNT% LEQ 1 ECHO COUNT=%COUNT% (Is Equal To 1)