batch file to search list of strings in txt files across multiple text files in folder & return counts in txt file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mrshiseido
Posts: 2
Joined: 15 Jun 2018 03:00

batch file to search list of strings in txt files across multiple text files in folder & return counts in txt file

#1 Post by mrshiseido » 17 Jun 2018 03:21

I am new to batch/script & been trying my best to create a batch that look up a list of mac addresses (in xxxx.xxxx.xxxx format as strings) in a text file & count the mac addresses occurrence from raw data text files in a folder. However, the code that I have tried doesn't seems to work properly or as expected. Your assistance & prompt feedback will be highly appreciated. Thanks in advance.

My OBJECTIVE is a batch that search for mac address (1 on each line) in text file, across a list of raw text files (4mb each) under particular folder (i.e. c:\test), count the total no of matches for each occurrence then write the total counts for each string in a separate text file.

@ECHO OFF

::: If this file exists, delete it
IF EXIST "Results.txt" DEL /Q /F "Results.txt"

::: Bare format DIR command listing ONLY filename.extension
DIR /B c:\201805\*.txt >> maps.txt

::: Set seq variable to 1 for the first sequence number
SET seq=1

::: Please go to command line and type FOR /? and look through there for the FOR /F explanations
::: This is saying for each line in strings.txt do a FIND /I for each string in maps.txt and if FIND /I finds a string, then CALL the SeqAdditionRoutine and pass the string found as the first argument to the CALL :label (:SeqAdditionRoutine in this instance)
FOR /F "TOKENS=*" %%S IN (mac.txt) DO (FIND /I "%%S" maps.txt && CALL :SeqAdditionRoutine "%%~S")
::: GOTO EOF needed here to pass control back to the CALLER or END once loop is complete to it doesn't move on to logic beneath which should only be called
GOTO :EOF

:SeqAdditionRoutine
::: This is saying FIND /I but with first argument passed as the string (same as above FIND /I but the first argument is passed here), and if successful (the double AND) ECHO the string equals 1 (or the sequence number variable value) to results.txt
FIND /I "%~1" maps.txt && ECHO %~1 = %seq% >> results.txt
::: This is saying (see SET /?) whatever seq variable is set to, ADD one to it and set it to this new value for whatever adding one to it will make it when it goes to EOF, it'll loop the next command (the CALLing loop) with this new value until it is successful in finding a strings and comes back down here again
SET /A seq=%seq%+1
::: GOTO EOF needed here to pass control back to the CALLER or END once loop is complete to it doesn't move on to logic beneath which should only be called
GOTO :EOF

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: batch file to search list of strings in txt files across multiple text files in folder & return counts in txt file

#2 Post by penpen » 21 Jun 2018 08:33

mrshiseido wrote:
17 Jun 2018 03:21
However, the code that I have tried doesn't seems to work properly or as expected.
I could interpret your objective in at least two ways, so it would help if you could give us a sample content of the folder, and related files, the output that your batch produces, and what you wanted to get instead.

penpen

Post Reply