Optimize Code/Pass Parameter Across Pipe:

March 10, 2010 at 23:04:43
Specs: Windows XP, 3.39 GHz

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=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


IF %COUNT% GTR 1 ECHO COUNT=%COUNT% (Is Greater Than 1)


Thanks much!

-Matt- :-)

See More: Optimize Code/Pass Parameter Across Pipe:

Report •

March 11, 2010 at 00:01:18
maybe, one way is make the execution of findstr conditional on the previous findstr:
findstr /L %computer% mathtestok.txt && findstr /L %computer% readingtestok && call :both
goto :eof
:: do whatever process here...

the second FINDSTR only activates if the first FINDSTR finds the string. the CALL :BOTH only activates if the second FINDSTR findst the string, so in effect:
IF found file1 AND found file2 CALL :BOTH
(if it's not found in file1, assuming no need to search file2.)

Report •

March 11, 2010 at 01:10:53
Thank you nbrane,

As I am only using two text files anyway, this idea would work.

-Matt- :-)

Report •
Related Solutions

Ask Question